This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.