Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is Executor Framework in Java and how to use it?

Last updated on Nov 25,2020 21K Views

10 / 22 Blog from Advance Java

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. 

Let’s begin!

What is the Executor Framework? Why use it?

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.

Types of Executors

There are mainly 4 types of Executors available. They are namely:

  • SingleThreadExecutor
  • FixedThreadPool
  • CachedThreadPool
  • ScheduledExecutor

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.

Best practices

  1. Always run your Java code against static analysis tools like PMD and FindBugs.
  2. Do note to cross-check and plan a better code to review the top lists in order to detect the possible deadlock or livelock in code during execution.
  3. In multi-threaded programs, make it a habit of catching errors then and there, not just exceptions.

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.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 27th April,2024

27th April

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is Executor Framework in Java and how to use it?

edureka.co