📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
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:
- The main method of this interface, named call(), may return a result.
- 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.
Executor Interfaces
- Executor - a simple interface that supports launching new tasks.
- ExecutorService - a subinterface of Executor, which adds features that help manage the lifecycle, both of the individual tasks and of the executor itself.
- ScheduledExecutorService - a subinterface of ExecutorService, supports future and/or periodic execution of tasks.
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. 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
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
Read more on Java Callable and Future Tutorial
Executors
Read more about Executors class on Executors Utility Class in Java
Check out the below examples demonstrates the usage of Executors class methods:
Comments
Post a Comment
Leave Comment