I have the following Runnable.run() method:
public void run() {
calculateStuff();
boolean ioSuccesful = IOforThisRunnable();
while(!ioSuccesful ) {
Thread.sleep(500); // alternative?
boolean ioSuccesful = IOforThisRunnable();
}
}
I want to execute this code in a ThreadPoolExecutor with a maximum number of Threads.
Now for my question:
Suppose my threadpool consist of 5 threads. In the current example 5 runnables would block execution for any additional Threads since Thread.sleep() does not release the thread?! (if IO is not succesful)
Is there any way to release the thread instead of using Thread.sleep() so that other runnables can start executing while another is waiting for IO?
So that in the worst case scenario all runnables would be sitting in while loop?
If your I/O work is blocking, then you have no choice: the thread has to be sitting around on the stack waiting for the blocking I/O to complete, so that thread can't be doing any other work.
What you want is to use non-blocking I/O, combined with something like Guava's
ListenableFuture
. This would enable you to do something like: