Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, 6 December 2016

What is thread pool ?


A thread pool is a collection of worker threads that efficiently execute asynchronous callbacks on behalf of the application. The thread pool is primarily used to reduce the number of application threads and provide management of the worker threads. Applications can queue work items, associate work with waitable handles, automatically queue based on a timer, and bind with I/O.

In thread pool ,group of threads are created, and when a service provider assign a task to thread, a thread is pulled from pool and do a task, after completion of task thread is again put into the pool. 

Using worker threads minimizes the overhead due to thread creation. Thread objects use a significant amount of memory, and in a large-scale application, allocating and deallocating many thread objects creates a significant memory management overhead.

Thread pools are often used in multi threaded servers. Each connection arriving at the server via the network is wrapped as a task and passed on to a thread pool. The threads in the thread pool will process the requests on the connections concurrently.

Java 5 comes with built in thread pools in the java.util.concurrent package, so you don't have to implement your own thread pool.

The thread pool architecture consists of the following:
  • Worker threads that execute the callback functions
  • Waiter threads that wait on multiple wait handles
  • A work queue
  • A default thread pool for each process
  • A worker factory that manages the worker threads


Example of ThreadPool :

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

class WorkerThread implements Runnable {

String msg = "";
public WorkerThread(String msg) {
this.msg = msg;
}
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+" : Thread Started, "+"Message : "+msg);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName()+" : Thread Ended");
}
}
public class ThreadPoolExample {

public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(7); //Thread Pool with 7 threads
for(int i=1;i<=10;i++){
Runnable wo = new WorkerThread(""+i);
executorService.execute(wo);
}
executorService.shutdown();
while(!executorService.isTerminated()){
}
System.out.println(" : All THREADS ARE EXECUTED : ");
}

}
Program Output:
pool-1-thread-2 : Thread Started, Message : 2
pool-1-thread-5 : Thread Started, Message : 5
pool-1-thread-3 : Thread Started, Message : 3
pool-1-thread-4 : Thread Started, Message : 4
pool-1-thread-1 : Thread Started, Message : 1
pool-1-thread-6 : Thread Started, Message : 6
pool-1-thread-7 : Thread Started, Message : 7
pool-1-thread-4 : Thread Ended
pool-1-thread-4 : Thread Started, Message : 8
pool-1-thread-6 : Thread Ended
pool-1-thread-6 : Thread Started, Message : 9
pool-1-thread-3 : Thread Ended
pool-1-thread-3 : Thread Started, Message : 10
pool-1-thread-2 : Thread Ended
pool-1-thread-5 : Thread Ended
pool-1-thread-7 : Thread Ended
pool-1-thread-1 : Thread Ended
pool-1-thread-4 : Thread Ended
pool-1-thread-3 : Thread Ended
pool-1-thread-6 : Thread Ended
 : All THREADS ARE EXECUTED : 


      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment