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?
-
6Where the underlying stream supports it, .NET uses IO completion ports and a ThreadPool thread for executing callbacks. – Jon Skeet Feb 26 '09 at 20:31
-
Jon, when might the underlying stream NOT support it? – Len Holgate May 04 '10 at 21:10
-
Any specific reason about why not to use java.nio? Actually, I am trying to implement some asynchronous i/o in my project at work and I haven't used either of these before. Hence wanted to know. Thanks. – Bhushan Jun 23 '11 at 17:31
-
I usually use http://async-io.org/games.html for java and you can get examples of code for game or chat. – Jose Pose S Jan 11 '17 at 19:54
6 Answers
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.
- 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.
- 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.
-
1
-
1Netty is asynchronous and event-driven like MINA. Check out the testimonials and performance reports written by real users in the home page. :) – trustin Dec 31 '09 at 00:56
-
It seems that Java 7 did indeed release with async I/O: http://openjdk.java.net/projects/nio/presentations/TS-5686.pdf – 700 Software Dec 02 '11 at 16:15
-
10
-
9@oconnor0 Non-blocking IO the kernel calls don't block under buffer-full(on writes) and buffer-empty(on reads), these states result in soft-error returns from the respective APIs.However non-blocking IO still copies data between user-space and kernel-space causing an unwanted extra copying of data which has a significant overhead when a lot of data is involved.The kernel then(may)copy the data yet again into packet size pieces with networking protocol overhead around it.Some network stacks/hardware drivers may support scatter gather to optimize the in kernel stages,but worst case is 3 copies. – Darryl Miles Aug 08 '12 at 16:53
-
11@oconnor0 Async IO accepts to remove the extra copying action during the data transition between user-space and kernel-space.This allows the kernel to directly access the data from user-space.To achieve this the application prepares memory and posts an IO request to the kernel.Control returns back to the application (the API is much like non-blocking).At some future point in time the kernel may access the user-space memory while performing the IO.Once done the kernel fires a signal like callback into user-space via the completion handler to notify the application of the IO result of request. – Darryl Miles Aug 08 '12 at 17:04
-
From a programming basis, non-blocking io (based usually on select) means that you still have to identify which sockets have incomming data on them. Async IO results in you getting a callback (or event) telling you the results of the IO action. There is also less kernel work to do, in theory asnyc io done in the kernal allows higher throughput and/or lower latency – Michael Shaw Dec 15 '12 at 15:00
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.

- 1,490
- 17
- 28
-
3Future is not true async. It's thread/semaphore based: http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html – Josmar Apr 14 '14 at 17:54
-
10What is "true async"? - Just because it is implemented using threads does not mean it is not asynchronous... – DejanLekic Dec 15 '15 at 13:00
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.

- 3,438
- 2
- 21
- 35
-
Naga actually seems like a very nice wrapper around the asynchronous stuff. – Kevin Read Feb 09 '10 at 22:18
-
1If you just want to do asynchronous Socket I/O without a framework getting in your way, Naga is what you want. – poindexter May 25 '11 at 15:31
-
-
I can confirm that naga works with Android. I haven't finished a production-quality app with it yet, but so far all my testing has been OK. – RenniePet Jul 29 '13 at 03:04
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.

- 161,610
- 92
- 305
- 395
-
1Your answer seems legit to me, but can you explain more? A little bit more explanations please. – Alireza Mohamadi Jan 05 '17 at 15:28
If you are interested in using it for Network Stuff. A really good choice is:
Have a look there its easy to use and very powerfull.
To the original question, the implementation only consumes a thread per I/O operation in one case, AsynchronousFileChannel on Unix/Linux systems.

- 2,807
- 3
- 22
- 6