CodesJava

Easy learning with example program codes

Executor framework in java


Executor framework provides the facility to separates the task creation and its execution. We have to pass Runnable objects to the executor and it is the responsibility of the executor to execute, instantiate and run with necessary threads.

Executor framework interfaces

  • Executor: It is used to submit a new task.
  • ExecutorService: it is a subinterface of Executor that adds methods to manage lifecycle of threads. It is used to run the submitted tasks and methods to produce a Future to get a result from an asynchronous computation.
  • ScheduledExecutorService: It is a subinterface of ExecutorService, to execute commands after a given delay.

How to create an executor?

The Executors classes are used to create the executor.

// It creates a single thread ExecutorService
ExecutorService singleExecutorService = Executors.newSingleThreadExecutor();
//It creates a single thread ScheduledExecutorService
ScheduledExecutorService singleScheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
//It creates an ExecutorService that use a pool of 5 threads
ExecutorService fixedExecutorService = Executors.newFixedThreadPool(5);
//It creates an ExecutorService that use a pool that creates threads on demand and kill them after 60 seconds if they are not used
ExecutorService onDemandExecutorService = Executors.newCachedThreadPool();
//It creates a ScheduledExecutorService that use a pool of 5 threads
ScheduledExecutorService fixedScheduledExecutorService = Executors.newScheduledThreadPool(5);

Example

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
public class ExecutorTest {
  public static void main(String[] args) {
	  ExecutorService executor = Executors.newFixedThreadPool(3);
      for (int i = 0; i < 5; i++) {  
          Runnable worker = new WorkerThread("" + i);  
          executor.execute(worker); 
        }  
      executor.shutdown();  
      while (!executor.isTerminated()) {   }  
 
      System.out.println("All threads are finished.");  
  }
}
 
class WorkerThread implements Runnable {  
    private String message;  
    public WorkerThread(String s){  
        this.message=s;  
    }  
     public void run() {  
        System.out.println(Thread.currentThread().getName()+" (Start) message = "+message);
        try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		} 
        System.out.println(Thread.currentThread().getName()+" (End)"); 
    }   
}

Output

pool-1-thread-1 (Start) message = 0
pool-1-thread-3 (Start) message = 2
pool-1-thread-2 (Start) message = 1
pool-1-thread-1 (End)
pool-1-thread-1 (Start) message = 3
pool-1-thread-2 (End)
pool-1-thread-2 (Start) message = 4
pool-1-thread-3 (End)
pool-1-thread-2 (End)
pool-1-thread-1 (End)
All threads are finished.

Related topics

Posted in Java   

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki