Difference between Executor, ExecutorService, and Executors class in Java!

Difference between Executor, ExecutorService, and Executors class in Java!


Thanks to the original writer and article : https://javarevisited.blogspot.com/2017/02/difference-between-executor-executorservice-and-executors-in-java.html#axzz7ZGX8FDUO

All three classes?Executor,?ExecutorService, and?Executors?are part of Java's?Executor framework ?which provides?thread pool?facilities to?Java?applications. Since the creation and management of Threads?are expensive and the operating system also imposes restrictions on how many Threads an application can spawn, it's a good idea is to use a pool of threads to execute?tasks in parallel, instead of creating a new thread every time a request comes in. This not only improves the response time of the application but also prevent resource exhaustion errors like?"java.lang.OutOfMemoryError: unable to create new native thread".


A?thread pool ?that is created when an application is a startup solves both of these problems. It has ready threads to serve?clients?when needed and it also has a bound on how many threads to create under load.

From Java 1.5, it was the application?programmer's responsibility to create and manage such thread pool but from JDK 5 onward Executor framework provides a variety of built-in thread pools in Java e.g. fixed thread pool which contains a fixed number of threads and cached thread pool which can spawn new threads when needed.

The main difference between?Executor,?ExecutorService,?and?Executors?class?is that Executor is the core interface which is an abstraction for parallel execution. It separates tasks from execution, this is different from?java.lang.Thread?class which combines both task and its execution. You can?read?the?difference between Thread and Executor ?to learn more differences between these two key classes of Java.

On the other hand,?ExecutorService?is an extension of the Executor?interface?and provides a facility for returning a Future object and terminate, or shut down the thread pool. Once the shutdown is called, the thread pool will not accept new tasks but complete any pending task. It also provides a?submit() ?method which extends?Executor.execute()?method and returns a Future.

The Future object provides the?facility?of asynchronous execution, which means you don't need to wait until the execution finishes, you can just submit the task and go around, come back and check if the Future object has the?result, if the execution is completed then it would have a result which you can access by using the?Future.get()?method. Just remember that this method is a?blocking method ?i.e. it will wait until execution finish and the result is available if it's not finished already.

By using the?Future object ?returned by?ExecutorService.submit()?method, you can also cancel the execution if you are not interested anymore. It provides a?cancel()?method to cancel any pending execution.

The third one?Executors?is a utility class similar to?Collections, which provides?factory methods ?to create different types of thread pools e.g. fixed and cached thread pools. Let's see some more differences between these three?classes.

And, if you are serious about mastering Java multi-threading and concurrency then I also suggest you take a look at the?Java Multithreading, Concurrency, and Performance Optimization?course ?by Michael Pogrebinsy on Udemy. It's an advanced course to become an expert in Multithreading, concurrency, and Parallel programming in Java with a strong emphasis on high performance.

No alt text provided for this image

Executor vs ExecutorService vs Executors in Java

As I told, all three classes are part of the Java 1.5 Executor framework, and it's very important for a Java programmer to?learn?and understand these classes to make effective use of different types of thread pools provided by Java.?Let's see some key differences between?Executor,?ExecutorService, and?Executors?in Java to understand them better:

1) One of the key differences between?Executor?and?ExecutorService?interface is that the former is a?parent?interface while?ExecutorService?extends?Executor?I mean, it's a sub-interface of Executor.

2) Another important difference between?ExecutorService?and?Executor?is that Executor defines?execute()?method which accepts an object of the?Runnable?interface, while?submit()?method can accept?objects of both?Runnable ?and?Callable ?interfaces.


3) The third difference between?Executor?and?ExecutorService?interface is that the?execute()?method doesn't return any result, its return type is void but the?submit()?method returns the result of?computation?via a?Future ?object. This is also the key difference between?submit()?and?execute()?method, which is one of the frequently asked?Java concurrency interview questions .


4) The fourth difference between?ExecutorService?and?Executor?interface is that apart from allowing a client to submit a task, ExecutorService also provides methods to control the thread pool e.g. terminate the thread pool by?calling?the?shutDown()?method. You should also read "Java Concurrency in Practice" ?to learn more about the graceful shutdown of a thread-pool and how to handle pending tasks.


5) Executors class provides?factory methods ?to create different kinds of thread pools e.g.?newSingleThreadExecutor()?creates a thread pool of just one thread,?newFixedThreadPool(int numOfThreads)?creates a thread pool of a fixed number of threads, and?newCachedThreadPool()?creates new threads when needed but reuse the existing threads if they are available.


Summary

Here is the summary of some key differences between Executor and ExecutorService in Java:


No alt text provided for this image

That's all about the?difference between Executor, ExecutorService, and Executors in Java. Remember, all three are part of the Executor framework. Java provides some out-of-box thread pool implementations like, a pool of fixed number of threads and pool of cached thread which can expand itself depending upon load.


The?ExecutorService?class provides the?submit()?method which can return the result of computation and Executor provides the?execute()?method which accepts a task which can be executed by the same thread, a thread pool, or another thread depending upon implementation.?

Ayyoub Al-Qaissi

Development and Production, Senior Manager

2 年

Nice article Omar, thanks!

回复
moujab zeidan

Head of Departmental System Development Unit | Databases, Technical Support, Software Development

2 年

Java language is a very efficient rich with features ….

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了