Java’s
concurrency library, java.util.concurrent, has a variety of useful classes to make concurrent code easier to write and more reliable. (If you’re not familiar with that library, you’ll find this post a lot more comprehensible if you read Sun’s
tutorial on concurrency.)
There are many ways to put the various building blocks that java.util.concurrent (j.u.c from now on) provides to create concurrent systems. Before we begin, here’s a short reminder of what the most basic j.u.c classes and interfaces are.
RunnableIntended to be more or less equivalent to starting a Thread. It defines a method that does not return anything and cannot throw checked exceptions.
CallableA more flexible variant of the same idea. It defines a method that can return a value and throw a checked exception.
FutureRepresents the result of a computation (e.g. a Callable). It is used in place of a value that may not be computed yet.
ExecutorAccepts Runnables and returns void.
ExecutorServiceExtends Executor to also provide a method that accepts a Callable and returns a Future.
ExecutorsProvides simple methods to get common types of ExecutorService implementations.
Some of them are:
CompletionService