Implementation of thread Pool in java

In my previous post of thread pool (https://www.techartifact.com/blogs/2009/06/what-is-thread-pool-in-java.html),I have given the theoritcal knowledge.now I came with code.Hope it will help a lot.
Java’s implementation of thread pool is based on an executor. Executor is a generic concept modeled by this interface.

Package java.util.concurrent;
Public interface Executor {
Public void execute(Runnable task);
}

You simple have to create the task and pass it on execute() method of an appropriate executor.The pool is an instance of ThreadPoolExecutor class.this class implements ExecutorService Interface which tell how to put task and how to shut down.
Example of creation of thread pool

package java.util.concurrent;
public class ThreadPoolExecutor implements ExecutorService {
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue,
                           ThreadFactory threadfactory);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue
                          RejectedExecutionHandler handler);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue workQueue
                          ThreadFactory threadfactory
                           RejectedExecutionHandler handler);

CorePoolSize , keepAliveTime, are the part by which we can manage the thread pool.we can use a constructor to create the task and put them in thread pool

Now we will create the task and we will put into the threadpool.here it’s the example.

import java.util.concurrent.*;
 public class ThreadPoolTest {
public static void main(String[] args) {
int nTask =Integer.parseInt(args[0]);
long n = ling.parseLong(args[1]);
int tpSize =Integer.parseInt(args[2]);

ThreadPoolExecutor tpe = new ThreadPoolExecutor(tpSize,tpSize,50000L,
                             TimeUnit.MILLISECONDS,new LinkedBlockingQueue());
Task[] tasks = new Task[nTask];
for(int i =0;itasks[i]= new Task(n,"Task " + i);
tpe.execute(task[i]);
}
tpe.shutdown();
}
}

This is implementation of thread Pool.It is easy to use.Any suggestion would be welcome.

Pin it