This thread-pool is called the Executor framework which relieved Java utility builders from the accountability of making and managing threads
The JDK 1.5 Executor framework is a mix of Executor, Executors, and ExecutorService interface to supply a completely purposeful, feature-rich thread pool in Java. By the way in which that was the basic distinction between the Thread and Executor idea in Java, let’s have a look at a few extra particulars about Thread and Executor to reply this query higher.
Thread vs Executor in Java
1) The firstly distinction between Thread and Executor is that java.lang.Thread is a class in Java whereas java.util.concurrent.Executor is an interface.
2) The Executor idea is definitely an abstraction over parallel computation. It permits concurrent code to be run in a managed approach. Then again, Thread is a concrete strategy to run the code in parallel.
3) The third distinction between an Executor and a Thread class is that the previous decouples a job (the code which must be executed in parallel) from execution, whereas within the case of a Thread, each job and execution are tightly coupled.
4) The Executor idea permits your job is to be executed by a employee thread from the thread pool, whereas Thread itself execute your job.
5) Executor gives a execute() methodology which accepts a Runnable job, whereas Thread accepts the Runnable job on its constructor.
6) Another key distinction between a Thread and an Executor is {that a} Thread can solely execute one Runnable job however an Executor can execute any variety of Runnable duties.
7) Within the case of Thread, the duty is executed by the Thread which accepts Runnable occasion, however within the case of Execution the command (a Runnable implementation) could also be executed in a brand new thread, a pooled thread, or within the calling thread itself, relying upon the implementation of Executor interface.
8) Within the case of a thread, it is the developer’s accountability to create and begin the thread, however within the case of Executor, the framework will create and begin threads for you. Although you may management the entire course of by giving your implementation of the Executor interface.
Although, with the enhancements in ForkJoinPool in Java 7 and eight, you would possibly need to use that as a substitute of Executor.
7) Now, let’s have a look at an instance of execution of a Runnable job through Executor and through Thread in Java:
import java.util.concurrent.Executor; import java.util.concurrent.Executors; public class Important { public static void essential(String args[]) { Runnable job = new Runnable() { @Override public void run() { System.out.println("Activity is executed by : " + Thread.currentThread().getName()); } }; Thread t = new Thread(job, "MY_THREAD"); t.begin(); Executor e = Executors.newSingleThreadExecutor(); e.execute(job); } } Output Activity is executed by MY_THREAD Activity is executed by pool-1-thread-1
The distinction is kind of clear that first is only a thread whereas later is a pool of threads.
It is value noting that manufacturing unit strategies of Executors class e.g. newSingleThreadExecutor() return an ExecutorService, which is a sub-interface of Executor and likewise gives strategies to accepts a Callable, terminate, or shut down the thread pool.
That is all concerning the distinction between a Thread and an Executor in Java. You may see that although each are associated to the parallel execution of duties they’re a separate abstraction. A Thread represents one thing which is answerable for executing your code in parallel, whereas an Executor is an abstraction for concurrent job execution.
Most significantly, Executor decouples the duty to its execution which implies an asynchronous execution is feasible, however job and execution are tightly coupled within the case of Thread.
Different multi-threading articles chances are you’ll like
- Prime 50 Java Multithreading Interview Questions from final 5 years (record)
- Prime 5 Programs to be taught Multithreading and Concurrency in Java (programs)
- 10 Multithreading and Concurrency Greatest Practices Java developer ought to comply with (article)
- Tips on how to use Future and FutureTask in Java? (tutorial)
- Tips on how to remedy the Producer-Shopper Drawback utilizing Lock and Situation (resolution)
- Is “Java Concurrency in Observe” Nonetheless Legitimate within the period of Java 8? (opinion)
- Tips on how to be part of greater than two Threads in Java? (instance)
- What’s the proper strategy to cease a Thread in Java? (tutorial)
- Prime 10 Programs to be taught Java in-depth (programs)
- Distinction between extends Thread and implements Runnable in Java? (reply)
- Prime 5 programs to Be taught Java Efficiency Tuning (programs)
- Distinction between the beginning() and run() methodology of Thread in Java? (reply)
- 10 Programs to Crack Java Interviews for Inexperienced persons (programs)
- Prime 12 Java Concurrency Questions for Skilled Programmers (see right here)
- Distinction between multi-threading and multi-tasking in Java? (reply)
- Prime 5 Books to be taught Java Concurrency in-depth (books)
- What’s happens-before in Java Concurrency? (reply)
- 6 Books to be taught Multithreading and Concurrency in Java (books)
- 10 Superior Core Java Programs for Skilled programmers (course)