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.
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
posted
0
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: 24
posted
0
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?
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.