I have a thread pool with fixed number of working threads (say 4). I continiously fetch new runnables to an executor. All of these runnables have a long period sleep call, waiting for another thread to be interrupted by it:
Runnable runnable = new Runnable() {
@Override
public void run() {
//do preparation
doPreWork();
//wait for some other runnable to interrupt me
try {
Thread.sleep(40000);
}
catch(InterruptedException e) {
}
//finish work
doAfterWork();
}
}
So the question is: when I fetch first 4 runnables to my executor, all working threads are sleeping and other incoming runnables(a lot of them, because they are continiously incoming) are queued and have to wait for available threads. Is there any way, I can use sleeping threads to execute new incoming runnables, maintaining others sleeping?
ExecutorServicebeing used. - Gray