Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Wednesday, 26 July 2017

Thread Pool In Java

Java Thread Pool

Java Thread Pool

Thread pool in java manages the pool of worker thread i.e Thread pool represents a group of worker threads that are waiting for the job and reuse many times.

Most of the executor implementations in java.util.concurrent use thread pool which consists of worker thread.

In thread pool environment, a group of fixed size thread are created.

This type of pool always has a specified numbers of thread running, if a thread is somehow terminated while it is still in use, it is automatically replaced with new thread.

Thread pool contains a queue that keeps tasks waiting to get executed and here to create a new thread pool in java we can use ThreaPoolExecutor. 

This kind of thread exists separately from the Runnable and Callable tasks it executes and it is used to execute multiple tasks.

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


Advantage of Java Thread Pool

  • It saves time because there is no need to create a new thread.

Usage of Thread Pool In Java

  • Java Thread pool mostly used in Servlet and JSP where container creates a Thread pool.

Java Thread Pool Example

Let's take look of simple example of java thread pool by using ExecutorService and Executors.

//WorkerThread.java

public class WorkerThread implements Runnable
{
private String command;
public WorkerThread(String s)
{
this.command = s;
}
public void run()
{
System.out.println(Thread.currentThread().getName()+"start.command = "+command);
processCommand();
System.out.println(Thread.currentThread().getName()+" End. ");
}
private void processCommand()
{
try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}

public String toString()
{
return this.command;
}
}




Java ExecutorService Example

This is the test program class ThreadPoolExample.java where we are creating fixed thread pool from Executors Framework.

//ThreadPoolExample.java
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample
{
public static void main(String args[])
{
ExecutorService executor = Executors.newFixedThreadPool(5);
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 thread");
}
}

output : pool 1 -1- thread-1 start.command = 0
              pool 1 -1- thread-2 start.command = 1
              pool 1 -1- thread-5 start.command = 4
              pool 1 -1- thread-4 start.command = 3
              pool 1 -1- thread-3 start.command = 2
              pool 1 -1- thread-1 End.
              pool 1 -1- thread-2 End.
              pool 1 -1- thread-2 start.command = 1
              pool 1 -1- thread-5 start.command = 6
              pool 1 -1- thread-5 End.
              pool 1 -1- thread-4 End.
              pool 1 -1- thread-5 start.command = 7
              pool 1 -1- thread-4 start.command = 8
              pool 1 -1- thread-3 End.
              pool 1 -1- thread-3 start.command = 9
              pool 1 -1- thread-2 End.
              pool 1 -1- thread-1 End.
              pool 1 -1- thread-5 End.
              pool 1 -1- thread-4 End.
              pool 1 -1- thread-3 End.
              finished all thread

In the above example, There are fixed size thread pool of 5 workers threads and then we submitting 10 jobs to this pool. Since the thread pool size is 5, it will start working on 5 jobs and other jobs will be in the wait state and as soon as one of the jobs is finished, another job from the wait queue will be picked up by the worker thread and get's executed.

Share:

1 comment:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate