Asynchronous IO in Java?

73,183

Solution 1

Java's NIO package (as of Java6), provides support for non-blocking I/O only, via Selectors. Java7 is hopefully going to ship with NIO.2, which includes asynchronous I/O support. Today, your best bet is to make use of a framework. ARMistice mentioned Mina. Here are some others.

  1. Grizzly. This is the I/O core for Sun's GlassFish server. Grizzly provides a facility for doing asynchronous reads/writes (via a queue model). It supports TCP and UDP alike. I've used Grizzly in a couple of projects. There are things I like and dislike about the framework, but to detail this is really another topic. I will say that it's quite easy to get something up and running and Grizzly does a lot of the heavy lifting for you.
  2. Netty. This project comes from one of the original authors of the Mina project. I haven't used this one so I don't know about its support for asynchronous I/O. You should take a look.

Now, with regard to your question about threads, NIO Selectors do not use threads for non-blocking I/O. In JDK6 they use select() under Windows and the epoll facility on newer Linux kernels. For asynchronous I/O, threading details depend on the framework.

Solution 2

JAVA 7 arrived so new answer is NIO.2 with Future class. Example :

On server side:

final AsynchronousServerSocketChannel serverSocket=
  AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("127.0.0.1", 2587)); // Listening on port 2587 for client connection
Future<AsynchronousSocketChannel> future= serverSocket.accept();
final AsynchronousSocketChannel clientSocket= future.get(); // now it's blocking, useful: future.isDone() and .isCancelled()

//Do whatever you want ..
InputStream stream = Channels.newInputStream(clientSocket) (...)

On client side:

AsynchronousSocketChannel clientChannel = AsynchronousSocketChannel.open();
Future connected = localSocket.connect(ourServerSocketAddress);
// later: if(future.isDone())
connected.get();

//Send something
OutputStream os = Channels.newOutputStream(clientChannel );
os.write (...)

Update: If you can use actor model then AKKA TCP IO would be even better.

Solution 3

Another suggestion in regards to libs would be Naga (http://naga.googlecode.com). It is a bit less like a framework and more like a library. It tries to look more like the ordinary java sockets, if that is your cup of tea. It's minimalistic compared to Grizzly, Mina and Netty.

Solution 4

java.nio is just a package - a collection of "dumb" classes - by itself it does not employ any use of threads. When used properly, such as in the Reactor design pattern, you can achieve proper, fully-scalable, asynchronous I/O.

Solution 5

To the original question, the implementation only consumes a thread per I/O operation in one case, AsynchronousFileChannel on Unix/Linux systems.

Share:
73,183
thr
Author by

thr

Updated on July 08, 2022

Comments

  • thr
    thr almost 2 years

    What options for async io (socket-based) are there in java other then java.nio? Also does java.nio use threads in the backround (as I think .NET's async-socket-library does, maybe it's been changed) or is it "true" async io using a proper select call?