Tuesday, 1 August 2017

Thread Pool in Java with example of array addition

                 In this post, we will see what is threadpool in Java along with an example of two arrays addition.

 What is Threadpool?

                 Threadpool is a concept in Java. It refers to the collection of threads i.e. a group of fixed size of threads.

How it works?

                 A thread is taken (i.e. pulled) from thread pool and task is allocated to it. Similarly, other threads are taken from thread pool and tasks are allocated to them. When task is completed, thread is returned to the thread pool. A returned thread in thread pool can be pulled back again and can be allocated a new task.

                 Suppose there are three threads in a thread pool and five tasks.
(First Thread=First Task) First thread will be allocated first task.
(Second Thread=Second Task) Second thread will be allocated second task.
(Third Thread=Third Task) Third thread will be allocated third task.

Once the first or second or third thread will be free i.e. completed task; it will return to thread pool and it will be allocated fourth task.

Again whoever thread gets free will return back to thread pool and will be allocated fifth task.


Advantage of Thread Pool:
                 Thread Pool reuses the threads. That's why, it reduces the time for creating new threads.
                 Java Thread Pool can be used with Servlet or JSP.

                 Go through the following example:

                 Here we are adding two arrays with 10 elements each. We are creating a thread pool with 5 threads.


import java.util.concurrent.*;  

class WorkerThread implements Runnable 
    private int num; 
    int x[]={1,2,3,4,5,6,7,8,9,10};
    int y[]={1,2,3,4,5,6,7,8,9,10};
    static int z[]=new int[10];    
    public WorkerThread(int j)
     public void run() 
        System.out.println("z["+num+"]=x["+num+"]+y["+num+"]"+" calculated by "+Thread.currentThread().getName()); 

public class ThreadPoolExample 
     public static void main(String[] args) 
        ExecutorService executor = Executors.newFixedThreadPool(5);//creating a pool of 5 threads  
        for (int i = 0; i < 10; i++) 
            Runnable worker = new WorkerThread(i);  
            executor.execute(worker);//calling execute method of ExecutorService  

       executor.shutdown();//shutdown() will not allow allocating new tasks to threads but will wait till the completion of all allocated tasks

       while (!executor.isTerminated()) { } 

       for (int i = 0; i < 10; i++) 
       System.out.println("Finished all threads");  

parag@parag-Inspiron-N4010:~/Desktop/prog$ javac ThreadPoolExample.java 
parag@parag-Inspiron-N4010:~/Desktop/prog$ java ThreadPoolExample
z[1]=x[1]+y[1] calculated by pool-1-thread-2
z[3]=x[3]+y[3] calculated by pool-1-thread-4
z[0]=x[0]+y[0] calculated by pool-1-thread-1
z[2]=x[2]+y[2] calculated by pool-1-thread-3
z[8]=x[8]+y[8] calculated by pool-1-thread-3
z[7]=x[7]+y[7] calculated by pool-1-thread-4
z[6]=x[6]+y[6] calculated by pool-1-thread-1
z[5]=x[5]+y[5] calculated by pool-1-thread-2
z[9]=x[9]+y[9] calculated by pool-1-thread-3
z[4]=x[4]+y[4] calculated by pool-1-thread-5
Finished all threads

Check other posts on Multi-Threading in this link http://www.comrevo.com/2016/09/multi-threading.html

No comments:

Post a comment