You can use some BlockingQueue .
When you read from a queue (in a stream), you either receive the next element, or if it is empty, wait until it is received.
This is actually you do not sleep the thread, but use the property of blocking the queue. For instance:
private BlockingQueue queue; @Override public void run() { while(true) { handle(queue.poll()); } }
The above code is in Runnable - you can use the ExecutorService to start in an executable, or old-fashioned way with Thread
The queue, of course, is set from the outside and filled (again from the outside) by incoming elements.
Bozho source share