How to tell if there is an available thread in a thread pool in java?

You should not submit a Thread object to the executor, that negates its entire purpose. You should submit Runnable objects and let the Executor worry about the Thread handling. It will automatically queue up your Runnable s when all threads are busy and when one task is complete it will grab a waiting task from the queue.

You should not submit a Thread object to the executor, that negates its entire purpose. You should submit Runnable objects and let the Executor worry about the Thread handling. It will automatically queue up your Runnables when all threads are busy and when one task is complete it will grab a waiting task from the queue.So your code should look more like this: ExecutorService executor = Executors.

NewFixedThreadPool(N); ResultSet results = getWaitingTasksStmt.executeQuery(); while( results.next() ) { executor. Submit(new RunnableInheritedClass(results) ) ); } executor.shutdown(); executor. AwaitTermination(10, TimeUnit.

MINUTES); This will allow 10 minutes for all tasks to complete, adjust as neccesary for your situatioin. Waiting forever is discouraged so think of some kind of reasonable timeout for your tasks.

The ExecutorService does all that work for you. If all of the threads are currently being used by other tasks, the new tasks will be placed into a queue and processed at some later time. Your main thread will not block when submitting a new task even if all the threads are currently being used.

ExecutorService executor = Executors. NewFixedThreadPool(N); ResultSet results; while( true ) { results = getWaitingTasksStmt.executeQuery(); while( results.next() ) { // If all threads are in use, the new task will be queued executor. Submit( new runnableInheritedClass(results) ); }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions