Exchanging Data

We will often want to exchange data between multiple cooperating threads. In the previous examples, we used Future and AtomicLong. Future is quite useful when we want to get a response from a task on completion. AtomicLong and the other atomic classes in the java.util.concurrent.atomic package are useful for dealing with single shared data values. Although these are useful for exchanging data, they can get unwieldy as we saw in the previous example. To work with multiple data values or to exchange data quite frequently, we’ll want a better mechanism than either of these provided. The java.util.concurrent API has a number of classes that provide thread-safe ways to communicate arbitrary data between threads.

If we simply want ...

Get Programming Concurrency on the JVM now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.