Working with the Thread class in Java can be very tedious and error-prone. For this reason, the Concurrency API was introduced back in 2004 with the release of Java 5 and has been enhanced with every new Java release. The API is located in the package java.util.concurrent. It contains a set of classes that make it easier to develop concurrent (multi-threaded) applications in Java.

The executor services are one of the most important parts of the Concurrency API. With the help of this guide, you can learn how to execute code in parallel via tasks and executor services in Java 8.

ExecutorService

The Concurrency API introduces the concept of an ExecutorService as a higher-level replacement for working with threads directly.

Executors are capable of managing a pool of threads, so we do not have to manually create new threads and run tasks in an asynchronous fashion.

Here, it submits a Runnable task for execution and returns a Future representing that task. Since Runnable is a functional interface, we are utilizing Java 8 lambda expressions to print the current threads name to the console.

ExecutorService Implementation

Since ExecutorService is an interface, it has to be implemented in order to make any use of it. The ExecutorService has the following implementations in the java.util.concurrent package:

ThreadPoolExecutor

ScheduledThreadPoolExecutor

Executors' factory class can also be used to create executor instances.