• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FixedThreadPool with a Blocking submit

 
Hadi Zeftin
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Hadi Zeftin
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 4181
21
IntelliJ IDE Java Python
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rudra tripathy
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Steve,

Thanks for the confirmation.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic