Threadpool


Threadpool

A collection of pre-initialized threads is known as a thread pool. The collection size is fixed but not mandatory.
The thread pool executors are of five types with pre-built methods in java.util.concurrent. Executors interface which can be created are listed below:

Some methods used for Threadpool are:

  • Fixed thread pool executor
  • Cached thread pool executor
  • Scheduled thread pool executor
  • Single thread pool executor

In a Thread Pool, a number of fixed-size threads are created. Whenever a task has to be performed, one of the threads is pulled out from collection of thread pool and assigned to that task by the service provider, and when the job is completed, the thread is returned.

Some methods of ThreadPoolExecutor class are listed below:

  • getPoolSize()
  • getActiveCount()
  • getCompletedTaskCount()
  • getLargestPoolSize()
  • shutdownNow()
  • isTerminated()

Risks in Thread Pool

  • Thread Leakage: when A thread is not returned back to the threadpool after a task has been performed.
  • Deadlock: The thread will be waiting in the queue for the output from the block of executing thread which leads to the unavailability of the thread for execution
  • Resource Thrashing: The number of threads required than the optimal number can cause problems which lead to resource thrashing.

Advantages of a Thread Pool

  • The performance is high
  • Time efficient
  • No need to create a thread over again
  • Convenient
  • Real-time usage

Disadvantages of the Thread Pool

  • There is no control and stable identity over the priority and state of the thread in progress.
  • When there is a high demand for the thread pool, the process may be deleted.
  • The thread pool cannot work well when two threads work simultaneously.

Example:

Com.knowledge2life WorkerThread.java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; 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); processmessage(); System.out.println(Thread.currentThread().getName()+" (End)");//prints thread name } private void processmessage() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } } } ThreadPoolExample.java class TestThreadPool { public static void main(String[] args) { ExecutorService executor = Executors.newFixedThreadPool(5); threads for (int i = 0; i < 10; i++) { Runnable worker = new WorkerThread("" + i); executor.execute(worker); } executor.shutdown(); while (!executor.isTerminated()) { } System.out.println("Finished all threads"); } }

OUTPUT:

pool-1-thread-1 (Start) message = 0
pool-1-thread-2 (Start) message = 1
pool-1-thread-3 (Start) message = 2
pool-1-thread-5 (Start) message = 4
pool-1-thread-4 (Start) message = 3
pool-1-thread-2 (End)
pool-1-thread-2 (Start) message = 5
pool-1-thread-1 (End)
pool-1-thread-1 (Start) message = 6
pool-1-thread-3 (End)
pool-1-thread-3 (Start) message = 7
pool-1-thread-4 (End)
pool-1-thread-4 (Start) message = 8
pool-1-thread-5 (End)
pool-1-thread-5 (Start) message = 9
pool-1-thread-2 (End)
pool-1-thread-1 (End)
pool-1-thread-4 (End)
pool-1-thread-3 (End)
pool-1-thread-5 (End)

Finished all thread