Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
Java always has strong support for concurrent and multi-threading programming. But in the beginning, until Java 5, the support was in the form of calling native constructs itself in the application layer. This was a disadvantage because you couldn’t handle the primitive calls more efficiently. So, this article on Executor Framework in Java brings up the solution to this problem.
The Executor Framework contains a bunch of components that are used to efficiently manage multiple threads. It was released with the JDK 5 which is used to run the Runnable objects without creating new threads every time and also mostly re-using the already created threads.
This Executor API de-couples the execution of a task from the actual task to be executed with the help of an Executor. This is centered around the Executor interface and its sub-interface ExecutorService and the class ThreadPoolExecutor.
By using this executor, only one has to implement the runnable objects and send them to the executor for execution.
Let’s take a look at an example.
Example:
public class Test implements Runnable { private String message; public Test(String message) { this.message = message; } @Override public String run() throws Exception { return "Hello " + message + "!"; } }
In this example, the Test class implements Runnable and is parameterized to type string. It is also declared to throw Exception. Also, note, this ability to throw an exception to the executor and the executor returning this exception back to the runner is of great importance because it helps the runner know the status of task execution.
Let’s move ahead to the next section of this article and take a look at the different types of Executor frameworks in Java.
There are mainly 4 types of Executors available. They are namely:
SingleThreadExecutor
This executor has only one thread and is used to execute tasks in a sequential manner. If any thread dies due to an exception while executing the task, a new thread is created to replace the old thread and the subsequent tasks are executed in the new thread.
FixedPoolExecutor
This is a pool of a fixed number of threads. The tasks submitted to the executor are executed by the “n” threads and suppose if there is more task to finish, they are stored on a LinkedBlockingQueue.
CachedThreadExecutor
This is mainly used when there are lots of short-lived parallel tasks on the line waiting to be executed. When compared with the fixed thread pool, here, the number of threads of this executor pool is not bounded. If all the threads are busy executing the assigned tasks and when there is a new task, it will create and add a new thread to the executor. If a thread remains idle for close to sixty seconds, they are terminated and removed from the cache.
ScheduledExecutor
This executor is used when you have a task that needs to be run at regular intervals or if in case you wish to delay a certain task. The tasks can be scheduled in ScheduledExecutor using either of the two methods scheduleAtFixedRate or scheduleWithFixedDelay.
Talking about the best practices to be followed, there are a few on the list.
With this, we come to the end of this blog on “Executor Framework in Java”. I hope you guys are clear with what has been taught to you in this article. We will keep digging the Java world together. Stay tuned!
Check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
Got a question for us? Please mention it in the comments section of this “Executor Framework in Java” blog and we will get back to you as soon as possible.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co