Thread Pool With executors Framework

  • Single Thread Executor : A thread pool with only one thread. So all the submitted tasks will be executed sequentially. Method :Executors.newSingleThreadExecutor()
  • Cached Thread Pool : A thread pool that creates as many threads it needs to execute the task in parrallel. The old available threads will be reused for the new tasks. If a thread is not used during 60 seconds, it will be terminated and removed from the pool. Method : Executors.newCachedThreadPool()
  • Fixed Thread Pool : A thread pool with a fixed number of threads. If a thread is not available for the task, the task is put in queue waiting for an other task to ends. Method : Executors.newFixedThreadPool()
  • Scheduled Thread Pool : A thread pool made to schedule future task. Method : Executors.newScheduledThreadPool()
  • Single Thread Scheduled Pool : A thread pool with only one thread to schedule future task. Method :Executors.newSingleThreadScheduledExecutor()

Program to understand the above:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* Following class is called a Job/Worker/Task
*
* @author ravisha
*
*/
class WorkerThread implements Runnable {
public void run() {
System.out.println(“This is a simple thread executor..”);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public class TestThreads {
public static void main(String[] args) {
/**
* Testing the behavior with Single tasks, Ideally Executors are not
* required for this kind of requirement.
*/
       testAllExecutors();
/**
* Testing the behavior with multiple tasks, Executors/Thread Pools are
* best suited for this kind of requirement.
*/
       testNewCachedThreadPool();
       testNewfixedThreadPool();

}

private static void testAllExecutors() {

// Creating one only Task/Worker/Job
Runnable runnable = new WorkerThread();
// Behavior is the same when you have single Task to do, irrespective of
// your executors (except scheduled executors, Schuled
// Executors will take some time delay to start)
ExecutorService[] executorServiceArray = {
Executors.newSingleThreadExecutor(),
Executors.newSingleThreadScheduledExecutor(),
Executors.newCachedThreadPool(),
Executors.newFixedThreadPool(20),
Executors.newScheduledThreadPool(10000) };

for (ExecutorService executorService : executorServiceArray) {
executorService.execute(runnable);
executorService.shutdown();
}

}

private static void  testNewfixedThreadPool() {
// Creating multiple Tasks/Workers/Jobs,
Runnable[] workers = new WorkerThread[10];
// There are 10 tasks , but let us use fixed thread pool having 5
// threads, so
// that at any point of time only five tasks will be executing.
ExecutorService executorService = Executors.newFixedThreadPool(20);

for (Runnable runnable : workers) {
runnable = new WorkerThread();
executorService.execute(runnable);
}
executorService.shutdown();
}

private static void testNewCachedThreadPool() {

// Creating multiple Tasks/Workers/Jobs
Runnable[] workers = new WorkerThread[10];
// There are 10 tasks , and we don’t want any limitation on threads to
// execute, go for cached thread pool
ExecutorService executorService = Executors.newCachedThreadPool();
for (Runnable runnable : workers) {
runnable = new WorkerThread();
executorService.execute(runnable);
}
executorService.shutdown();
}

}

Leave a comment