*
The moose likes Threads and Synchronization and the fly likes FixedThreadPool with a Blocking submit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "FixedThreadPool with a Blocking submit" Watch "FixedThreadPool with a Blocking submit" New topic
Author

FixedThreadPool with a Blocking submit

Hadi Zeftin
Greenhorn

Joined: Jun 03, 2010
Posts: 8
Hi, been searching a whole days now, and still haven't found any answers I can understand of.



I thought this would be a simple question with hopefully a simple answer too. How can I make the main/current thread block if the executor already working on a number of maxThread threads, for example if maxThreads=10 then exec.execute() will block if already submitting 10 threads and all of those threads are still running, until there some space available (i.e. some threads finish and idle). why ? because this.transaction.getNextTransaction() may always returning a result for a long time, putting it to executor will probably cost memory usage ?? not sure, just doesn't feel right.

maybe I'm using a wrong set of Classes there, if I am please point me to the right direction.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 407
    
    1

You could use the CallerRunsPolicy if that's helpful (not blocking though :-( ) ...

Anyway failing that (from Java Concurrency in Practice (very recommended book) ) there is
no predefined saturation policy to make execute block when the work queue is full. However the effect can be accomplished by using a Semaphore to bound the task injection rate
. It has a listing to achieve that sure you can google one.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
Hadi Zeftin
Greenhorn

Joined: Jun 03, 2010
Posts: 8
yes it does, thanks so much for the hint. now it became like this



and for anyone that had the same question, the bounded executor
rudra tripathy
Greenhorn

Joined: May 26, 2004
Posts: 25
Doesn't thread pool take care of that?if my thread pool size is 10, and i have 100 threads to execute, then this should automatically take care of,right, or i misunderstood the question?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

rudra tripathy wrote:Doesn't thread pool take care of that?if my thread pool size is 10, and i have 100 threads to execute, then this should automatically take care of,right, or i misunderstood the question?


An ExecutorService will usually have two parts: 1 Pool of Threads and 1 Queue for holding tasks. When there are more tasks than threads, then the tasks get pushed into the Queue for execution when a thread becomes available. But the the thread which submits the task does not wait for a worker thread to become available - the task just goes into a Queue and the submitter continues on.

So if there are 10 threads already working, then the 11th task gets submitted, this extra task gets put into the Queue and the thread which submits the task continues on its way. What the OP wants is the submitter thread to block - stop executing - until the task gets consumed by a worker thread.


Steve
rudra tripathy
Greenhorn

Joined: May 26, 2004
Posts: 25
Hi Steve,

Thanks for the confirmation.
 
wood burning stoves
 
subject: FixedThreadPool with a Blocking submit
 
Similar Threads
Process the multiple records in a file by Producer/consumer concept using Multithreading
tomcat maxthread count and minspare thread count on tomcat 6.0
Required help in thread
How to only run a few threads at a time...
How to make my JMS Subscriber more scalable?