Difference between threadpoolexecutor and executorservice
Answers
Answered by
4
,ExecutorService, and Executers are part of Java's Executor framework which providesthread pool facilities to Java applications. Since creation and management of Threads are expensive and operating system also imposes restrictions on how many Threads an application can spawn, it's a good idea is to use a pool of thread to execute tasks in parallel, instead of creating a new thread every time a request come in. This not only improves the response time of application but also prevent resource exhaustion errors like "java.lang.OutOfMemoryError: unable to create new native thread". A thread pool which 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 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 task from execution, this is different fromjava.lang.Thread class which combines both task and its execution. You can read thedifference between Thread and Executor to learn more differences between these two key classes of Java.
From Java 1.5, it was 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 task from execution, this is different fromjava.lang.Thread class which combines both task and its execution. You can read thedifference between Thread and Executor to learn more differences between these two key classes of Java.
Similar questions