Java Reference
In-Depth Information
Chapter 9. Lambda-Enabled
Concurrency
I've already talked a bit about data parallelism, but in this chapter I'm going to cover how we
can use lambda expressions to write concurrent applications that efficiently perform message
passing and nonblocking I/O.
The principles are more general, though, and can be used in other frameworks and in your
own code without you necessarily needing frameworks at all.
Why Use Nonblocking I/O?
When I introduced parallelism, I talked a lot about trying to use a lot of cores efficiently.
That approach is really helpful, but when trying to process a lot of data, it's not the only
threading model that you might want to use.
Let's suppose you're trying to write a chat service that handles a very high number of users.
Every time a user connects to your service, a TCP connection to your server is opened. If you
follow a traditional threading model, every time you want to write some data to your user,
you would call a method that sends the user the data. This method call would block the
thread that you're running on.
This approach to I/O, called
blocking I/O
, is fairly common and fairly easy to understand be-
cause the interaction with users follows a normal sequential flow of control through your
program. The downside is that when you start looking at scaling your system to a significant
number of users, you need to start a significant number of threads on your server in order to
service them. That approach just doesn't scale well.
Nonblocking I/O
—or, as it's sometimes called,
asynchronous I/O
—can be used to process
many concurrent network connections without having an individual thread service each con-
nection. Unlike with blocking I/O, the methods to read and write data to your chat clients re-
turn immediately. The actual I/O processing is happening in a separate thread, and you are
free to perform some useful work in the meantime. How you choose to use these saved CPU