Thread pool executor service



How to work thread pool executor service





In Java what I know is, we can't call a thread more than once:

Thread t = new Thread; //Some Runnable
t.start();

t.start(); //Illegal and throw Exception at runtime.

 
As far as I know, it throws exception when you call t.start() again because the associated stack for the thread is destroyed once it goes out of run() 
method and you are trying to initialize things again.

In that case, what I know about thread pool is, it gives better performance & saves time because there is no need to create new thread (I read in this).

If there is no need to create new thread in thread pool scenario, then how it works with same thread which just finished its run method, 
will that thread can be used again?

I read this, and it says that <b>"Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. 
This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks."</b>

So what is Worker thread here, is it something different then normal Java threads?

With this link, I got something but still confused on what kind of stuff can be eliminated when we use thread pool and why it gives better 
performance than using normal java threads.

So can we say like this,

Thread has three parts,

Creation (Telling OS that it is new thread, create stack for it.)
Execute Runnable with run() method.
Destroying threads.




package com.kartik.thread.pool;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 
 * @author kmandal
 * 
 * 
 * In Java what I know is, we can't call a thread more than once:

Thread t = new Thread; //Some Runnable
t.start();

t.start(); //Illegal and throw Exception at runtime.

 
As far as I know, it throws exception when you call t.start() again because the associated stack for the thread is destroyed once it goes out of run() 
method and you are trying to initialize things again.

In that case, what I know about thread pool is, it gives better performance & saves time because there is no need to create new thread (I read in this).

If there is no need to create new thread in thread pool scenario, then how it works with same thread which just finished its run method, 
will that thread can be used again?

I read this, and it says that <b>"Most of the executor implementations in java.util.concurrent use thread pools, which consist of worker threads. 
This kind of thread exists separately from the Runnable and Callable tasks it executes and is often used to execute multiple tasks."</b>

So what is Worker thread here, is it something different then normal Java threads?

With this link, I got something but still confused on what kind of stuff can be eliminated when we use thread pool and why it gives better 
performance than using normal java threads.

So can we say like this,

Thread has three parts,

Creation (Telling OS that it is new thread, create stack for it.)
Execute Runnable with run() method.
Destroying threads.
 *
 */
public class TestThreadPool {
 // Maximum number of threads in thread pool 
    static final int MAX_T = 3;              
  
    public static void main(String[] args) 
    { 
        // creates five tasks 
     Task r1 = new Task("task 1",4); 
     Task r2 = new Task("task 2",3); 
        Runnable r3 = new Task("task 3",6); 
        Runnable r4 = new Task("task 4",9); 
        Runnable r5 = new Task("task 5",18);
        Runnable r6 = new Task("task 6",4); 
        Runnable r7 = new Task("task 7",3); 
        Runnable r8 = new Task("task 8",6); 
        Runnable r9 = new Task("task 9",9); 
        Runnable r10 = new Task("task 10",4); 
        Runnable r11 = new Task("task 11",3); 
        Runnable r12 = new Task("task 12",6); 
        Runnable r13 = new Task("task 13",9); 
        Runnable r14 = new Task("task 14",4); 
        Runnable r15 = new Task("task 15",3); 
        Runnable r16 = new Task("task 16",6); 
        Runnable r17 = new Task("task 17",9); 
          
        // creates a thread pool with MAX_T no. of  
        // threads as the fixed pool size(Step 2) 
        ExecutorService pool = Executors.newFixedThreadPool(MAX_T);   
         
        // passes the Task objects to the pool to execute (Step 3) 
        pool.execute(r1); 
        pool.execute(r2); 
        pool.execute(r3); 
        pool.execute(r4); 
        pool.execute(r5); 
        pool.execute(r6); 
        pool.execute(r7); 
        pool.execute(r8); 
        pool.execute(r9);
        pool.execute(r10);
        pool.execute(r11); 
        pool.execute(r12); 
        pool.execute(r13); 
        pool.execute(r14);
        pool.execute(r15);
        pool.execute(r16);
        pool.execute(r17);
          
        // pool shutdown ( Step 4) 
        pool.shutdown();     
    } 
}

package com.kartik.thread.pool;
//Java program to illustrate  
//ThreadPool 
import java.text.SimpleDateFormat;  
import java.util.Date; 

//Task class to be executed (Step 1) 
public class Task implements Runnable    
{ 
 private String name; 
 private int data;
   
 public Task(String s,int d) 
 { 
     name = s; 
     data=d;
 } 
   
 // Prints task name and sleeps for 1s 
 // This Whole process is repeated 5 times 
 public void run() 
 { 
     try
     { 
           
        System.out.println("Executing Time for task name - "+name +" and factorial data = " +factorial(data)+" thread name "+Thread.currentThread().getName());
        Thread.sleep(100); 
        System.out.println(name+" complete"); 
     } 
       
     catch(InterruptedException e) 
     { 
         e.printStackTrace(); 
     } 
 } 
 
 
//method to find factorial of given number 
 static long factorial(long n) 
 { 
     if (n == 0) 
       return 1; 
       
     return n*factorial(n-1); 
 } 
} 


Executing Time for task name - task 1 and factorial data = 24 thread name pool-1-thread-1
Executing Time for task name - task 3 and factorial data = 720 thread name pool-1-thread-3
Executing Time for task name - task 2 and factorial data = 6 thread name pool-1-thread-2
task 1 complete
task 2 complete
task 3 complete
Executing Time for task name - task 5 and factorial data = 6402373705728000 thread name pool-1-thread-2
Executing Time for task name - task 4 and factorial data = 362880 thread name pool-1-thread-1
Executing Time for task name - task 6 and factorial data = 24 thread name pool-1-thread-3
task 6 complete
task 4 complete
Executing Time for task name - task 7 and factorial data = 6 thread name pool-1-thread-3
task 5 complete
Executing Time for task name - task 8 and factorial data = 720 thread name pool-1-thread-1
Executing Time for task name - task 9 and factorial data = 362880 thread name pool-1-thread-2
task 8 complete
task 7 complete
task 9 complete
Executing Time for task name - task 11 and factorial data = 6 thread name pool-1-thread-3
Executing Time for task name - task 10 and factorial data = 24 thread name pool-1-thread-1
Executing Time for task name - task 12 and factorial data = 720 thread name pool-1-thread-2
task 10 complete
task 11 complete
task 12 complete
Executing Time for task name - task 14 and factorial data = 24 thread name pool-1-thread-3
Executing Time for task name - task 13 and factorial data = 362880 thread name pool-1-thread-1
Executing Time for task name - task 15 and factorial data = 6 thread name pool-1-thread-2
task 13 complete
task 14 complete
task 15 complete
Executing Time for task name - task 17 and factorial data = 362880 thread name pool-1-thread-3
Executing Time for task name - task 16 and factorial data = 720 thread name pool-1-thread-1
task 16 complete
task 17 complete




    
Thread_pool_image
Thread_pool_image



Other Links:
Remote Command-line debugger (Sun’s JDB)
ALGORITHM FOR HASHMAC
ASYMETRIC: ENCRYPTION
SYMETRIC: ENCRYPTION AND DECRYPTION BY SAME KEY
GENERATING KEYSTORE FILES









Previous
Next Post »