Implementation of thread Pool in java

By Vinay | June 10, 2009 | 7,468 views
Category Java, Oracle ADF


About author  I am Oracle/Java professional.Working on J2EE technologies and i.e Oracle ADF,Java,J2ee,PL/sql,Top Link,Apps for 2+ years.I am passionate about learning new technologies.I am sharing my knowledge. Give your views and suggestion. http://www.linkedin.com/in/vinaykumar2 Read more from this author


In my previous post of thread pool (http://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<Runnable> workQueue);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                           ThreadFactory threadfactory);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue<Runnable> workQueue
                          RejectedExecutionHandler handler);
public ThreadPoolExecutor(int CorePoolSize,int maximumPoolSize,
                           long keepAliveTime,timeUnit unit,
                          BlockingQueue<Runnable> 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<Runnable>());
Task[] tasks = new Task[nTask];
for(int i =0;i<ntask;i++){
tasks[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.

  • Share/Save/Bookmark
Read more post on Implementation of thread Pool in java Java Oracle ADF thread pool threadexecutor 

9 comments | Add One

Comments

  1. pauloNo Gravatar - 06/10/2009 at 11:34 am

  2. How to Make Thousands of Dollars Posting Links on GoogleNo Gravatar - 06/10/2009 at 2:30 pm

    Thanks for posting, I’ll definitely be subscribing to your blog.



  3. Peter VeentjerNo Gravatar - 06/11/2009 at 4:45 am

    You have not created an implementation of the Executor but you have only used an existing implementation.

    I was hoping that your blogpost was a littl bit better than the previous one, but it is just as bad.

    And the previous comment, is just a spam engine, not someone that has really read your post.



  4. vinayNo Gravatar - 06/11/2009 at 4:52 am

    hey peter.Well first of all i have not said that i have created an implementation of executor.yes i am explaining existing one.I am sharing my knowledge.If you like please read, if you don;t please dont read it.some information can be useful for you.Some information will not.But you can’t comment on the level of blog.And you don’t need to give updates on the comments .

    Hope this information will be better for others.Criticizing is always very easy.



  5. Mario GleichmannNo Gravatar - 06/11/2009 at 8:53 am

    Sorry Vinay,

    but i have to concede a point to peter: the title of your post is really misleading. I visited your post exactly because of this, hoping to see some internals about how you would ‘implement a thread pool in Java’ (as the title says).

    Nevertheless … appreciate your efforts!

    Greetings

    Mario



  6. Stewart SmithNo Gravatar - 08/20/2009 at 10:24 am

    I agree with Peter. I find these post annoying as they don’t add anything to what is well documented elsewhere and understood by most, if not all, Java programmers.

    What is even more annoying is that your friend keeps recommending the posts on LinkedIn.



  7. VinayNo Gravatar - 08/20/2009 at 10:53 pm

    @ stewart

    i don’t understand y this post is annoying to you.If u hve understanding of smthng ,it is not necessary that everyone is have understood this concept very well.I just try to give point to point information for newbie in java.If you know smethng that does’nt mean you should comment on everythnng.It will be better if you can add some information in thread pool.That will be really appreciated.But criticizing is really bad.
    For you information there is no friend in linkein who recommend this.Ok……………….



  8. shanejoesNo Gravatar - 02/14/2010 at 6:24 pm

    All the latest [url=http://www.weinthemix.com]music[/url] mixtapes from your favorite Djs.

    Visit: http://www.weinthemix.com



Trackbacks

  1. PimpThisBlog.com

Leave a Comment

Name:

E-Mail :

Website :

Comments :