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


Threads are a very important Part of Java, but creating large numbers of threads can degrade the performance of application.By thread pools, which allow you to maintain fixed number of threads running assigned tasks to each of the threads.
Suppose A thing may have to visit tens of thousands of pages, and you will not create tens of thousands of thread because it is an additional problem. For overcome this problem we need a thread pool. Thread pool give some fixed number of thread and these thread will do task. Once a thread finish one task, then it is assigned to new task. We will not create a new thread always. This is way thread pool will work. Developer has to implement this feature.
In Java:
• A thread pool implementation is provided in the form of the ThreadPoolExecutor class, introduced in Java 1.5;
• you can put in different implementations of BlockingQueue to specify different queue behavior such as queue bounds or priority ordering

Implementation of Thread pool

This can be done by like this

   public class ThreadPool extends java.lang.Object implements ThreadPoolInt

This class is generic implementation of thread pool which have following input
1) Size of the pool to be constructed
2) Name of the class which implements Runnable (which has a visible default constructor)

It constructs a thread pool with active threads that are waiting for activation. Once the threads have finished their task they will come back and wait for assigning a new task.

Benefits of Thread Pooling
• It saves the machine work of creating a new thread.
• Single thread can recycle again and again.
• The size of thread pool is given in starting .We can expand on the designs presented in this chapter to include a method to support growing the size of the pool at runtime if you need this kind of dynamic tuning.
• Response time can be quick.

Risks of using thread pools-
The disadvantage of thread pool is that. Suppose if an task is rejected because the thread pool is empty. There will be high rejection rate.Or if task on wait state then waiting time would be too long. Sometime there is problem of deadlock also.

References

http://www.japisoft.com/jservices/javadoc/jason/service/pool/ThreadPool.html

http://java.sun.com/developer/Books/javaprogramming/threads/chap13.pdf

18 Responses to Thread Pool in java


  1. vidhya dharanNo Gravatar
    Jun 05, 2009

    Nice artile dude,,
    Keep continue..
    cheers..


  2. piskodrochoNo Gravatar
    Jul 10, 2009

    I want to listen good music!


  3. HydrolyzeNo Gravatar
    Oct 15, 2009

    Just wanted to say hello all. This is my first post.

    I came to learn a ton here.


  4. OppoxPextNo Gravatar
    Nov 05, 2009

    Hey all i was just wondering if you were interested in watching the new saw movie? I just watched it here for free and it was prty damn amazing
    quality for free! http://tinyurl.com/NewSawMovie

    So if you havent seen this movie then check it out here! Also please dont share this link too much outside of this forum as it will probably die then.


  5. SoundJohnNo Gravatar
    Nov 21, 2009

    Hey! Thanx for this beautiful place of the Inet!!


  6. CypeapeddyZenNo Gravatar
    Dec 17, 2009

    Your welcome everyone,
    My computer worked not correctly, too much mistakes and buggs. Help me, please to fix buggs on my PC.
    I used Win Vista.
    Thx,
    CypeapeddyZen


  7. CialisNo Gravatar
    Mar 03, 2010

    HDQQNg Thank you for the material. Do you mind if I posted it in her blog, of course, with reference to your site?


  8. BlahNo Gravatar
    Sep 21, 2010

    What article??


  9. Rajesh KumarNo Gravatar
    Jan 12, 2011

    Thanks for beautifully explaining regarding thread pool.


  10. venkatNo Gravatar
    Feb 03, 2011

    short , cute and nice .


  11. BalaNo Gravatar
    May 09, 2011

    Article explain precisely to understand the concept of ThreadPool.
    Keep up the good work..


  12. jhonsonNo Gravatar
    Jul 15, 2011

    i want phone no of you for problem solving
    this is very completed no one can understand your article
    so dont write it again


  13. DavidNo Gravatar
    Aug 17, 2011

    JAVA 7 is slicker, has a pool of N actual threads dispatched to a pool of thread tasks. http://download.oracle.com/javase/7/docs/api/java/util/concurrent/ForkJoinPool.html


  14. James WhiteNo Gravatar
    Aug 26, 2011

    This is a good article on Thread Pools. I also wanted to mention an interface that goes along with this topic. The Callable interface in the java.util.concurrent package. This allows you to return a result through its call() method. I found this interface to be invaluable in the batch file processing application I work on.


  15. EmmaTENo Gravatar
    Nov 18, 2011

    Hi there,

    Just saying hello to the forum.

    Emma

    ============
    blackberry bold


  16. gamesgirlNo Gravatar
    Jan 06, 2012

    Wowo,Fantastic article,it’s so useful to me,and your post is very nice,I’ve learned a lot from your blog there,Keep on going,my friend,I will keep an eye on it.btw,thanks for your post

Trackbacks/Pingbacks

  1. PimpThisBlog.com
  2. Implementation of thread Pool in java - TechArtifact

Leave a Reply