Java Concurrency Tutorial

In this tutorial, we will learn high-level concurrency features introduced with version 5.0 of the Java platform. Most of these features are implemented in the new java.util.concurrent packages.

In the previous tutorial Java Multithreading Tutorial, we have learned low-level APIs that have been part of the Java platform from the very beginning. These APIs are adequate for very basic tasks.


In this tutorial, we only focus on high-level concurrency features that is Executor framework introduced with version 5.0 of the Java platform. If you want to learn the basics then check out Java Multithreading Tutorial.


With an executor, we only have to implement the Runnable objects and send them to the executor. The executor is responsible for their execution, instantiation, and running with necessary threads. But it goes beyond that and improves performance using a pool of threads. When you send a task to the executor, it tries to use a pooled thread for the execution of this task, to avoid continuous spawning of threads.


Another important advantage of the Executor framework is the Callable interface. It's similar to the Runnable interface, but offers two improvements, which are as follows:

  1. The main method of this interface, named call(), may return a result.
  2. When you send a Callable object to an executor, you get an object that implements the Future interface. You can use this object to control the status and the result of the Callable object.
Let's understand the Executor Framework in-depth with all the interfaces and it's implementation.

Executor Interfaces 

The java.util.concurrent package defines three executor interfaces:
  1. Executor - a simple interface that supports launching new tasks.
  2. ExecutorService - a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.
  3. ScheduledExecutorService - a subinterface of ExecutorService, supports future and/or periodic execution of tasks.
Let's briefly understand these interfaces with an example.

1. The Executor Interface

An object that executes submitted Runnable tasks. This interface provides a way of decoupling task submission from the mechanics of how each task will be run, including details of thread use, scheduling, etc. An Executor is normally used instead of explicitly creating threads. 

For example, rather than invoking new Thread(new(RunnableTask())).start() for each of a set of tasks, you might use:
 Executor executor = anExecutor;
 executor.execute(new RunnableTask1());
 executor.execute(new RunnableTask2());
 ...

2. ExecutorService Interface in Java

In this article, we will learn the ExecutorService interface supplements execute with a similar, but more versatile submit method. Like execute, submit accepts Runnable objects, but also accepts Callable objects, which allow the task to return a value. The submit method returns a Future object, which is used to retrieve the Callable return value and to manage the status of both Callable and Runnable tasks.
Read more on ExecutorService Interface in Java

3. ScheduledExecutorService Interface in Java

In this article, the ScheduledExecutorService interface that can schedule commands to run after a given delay, or to execute periodically.
The schedule() methods create tasks with various delays and return a task object that can be used to cancel or check execution. The scheduleAtFixedRate() and scheduleWithFixedDelay() methods create and execute tasks that run periodically until cancelled.
Read more on  ScheduledExecutorService Interface in Java

Future and Callable Interfaces 

Future Interface in Java

Future is a generic interface that represents the value that will be returned by a Callable object. Because this value is obtained at some future time, the name Future is appropriate.
Read more about Future interface at Future Interface in Java

Java Callable and Future Tutorial

In this tutorial, we will learn how to use Callable and Future interfaces together to execute the tasks and obtain the result and store it in the future object.

Executors

This class provides factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and Callable interfaces and classes.
Read more about Executors class on Executors Utility Class in Java

Check out the below examples demonstrates the usage of Executors class methods: 

1. Executors newSingleThreadExecutor Method Example

In this tutorial, we will learn about the Executor’s newSingleThreadExecutor factory method.

2. Executors newFixedThreadPool Method Example

In this article, we will learn about the Executor’s newFixedThreadPool factory method.

3. Executors newCachedThreadPool Method Example

In this article, we will learn about the Executor’s newCachedThreadPool factory method.

4. Executors newScheduledThreadPool Method Example

In this tutorial, we will learn about the Executor’s newScheduledThreadPool factory method.

5. Executors newSingleThreadScheduledExecutor() Method Example

In this article, we will discuss Executors newSingleThreadScheduledExecutor() method with examples.

Comments