Results 1 to 6 of 6
- 04-03-2010, 04:27 PM #1
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
How does ExecutorService + newFixedThreadPool "reuse" threads?
Hello,
I'm trying to fully understand how ExecutorService + newFixedThreadPool work. I understand the example on the Java API, and have made a few test applications of my own... but what I don't understand is how they are able to reuse threads.
In a test application of my own, I made an instance of newFixedThreadPool with a pool size of 3. I then ran about 10000 small Callables through it. Through the use of println()'s and observation through jconsole, I was able to confirm that the pool did in fact only create 3 threads to run through all the callables I was sending it.
From what I understand about threads, isn't a thread tied to a single Runnable? Therefore, how are the same 3 threads being reused to run 10000 callables?
Any insight is appreciated, this is really bugging me :)
- 04-03-2010, 05:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Read the example in the API documentation for the ExecutorService interface. A thread pool creates its own threads and supplies its own clever little Runnables for those threads. Those Runnables never end but synchronize on a queue (they wait()) until a Callable is present in that queue; they are notified when that happens and their Runnable runs the Callable from the queue and the entire scenario repeats itself again.
As you can see in the example the Runnable has an infite loop where it executes stuff from a queue. The pool manages the threads (it creates more of them if allowed etc.)
kind regards,
Jos
- 04-03-2010, 05:42 PM #3
Member
- Join Date
- Apr 2010
- Posts
- 2
- Rep Power
- 0
Yeah shortly after I posted started this Thread (pun intended), it kind of "ticked" in my head what actually must be going on. The threads in the pool aren't being created with the Callables being submitted into the pool for execution, but rather using a Runnable internally specified by the thread pool that wraps around an internal queue built up with the Callables.
Thanks for the reply!
- 04-03-2010, 05:53 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 06-06-2011, 07:39 AM #5
Member
- Join Date
- Jun 2011
- Posts
- 1
- Rep Power
- 0
Concern with ThreadPool
Hi Friend,
I have one doubt.
say i have 100 runnables, but only 3 threads in pool.
my question is how these 3 threads are picking the 100 runnable one after the other.
see what i know is once a thread is created with one runnable, it will die once it is finished.
then how the thread is able to pick the next runnable.
is there any way to setRunnable to the existing thread,
Expecting your comments on the same.
- 06-06-2011, 10:05 AM #6
Dharma: you just quoted Jos answering your question!
Jos: Doug Lea also wrote a really great book, Concurrent Programming in Java: Design Principles and Patterns. Best Java book I ever read.Get in the habit of using standard Java naming conventions!
Similar Threads
-
Java, Military Format using "/" and "%" Operator!!
By sk8rsam77 in forum New To JavaReplies: 11Last Post: 02-26-2010, 03:03 AM -
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
how to override "cancel operation" in "progress bar"
By singswt in forum SWT / JFaceReplies: 2Last Post: 10-08-2009, 11:28 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks