glow060197
asked on
unblocking a read on System.in
how can i unblock a read on System.in and other InputStreams, from a different thread or whatever?
thanks
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just want to point out that there may be a synchronization problem. When 2 threads are working on the same stream object, one read may lock the object and disallow the other to close it.
Neither the "read" nor the "close" methods on a stream are synchronized.
Alternatively, you can change the read loops to only invoke read when there are bytes pending to be read, i.e. available returns non-zero.
boolean done = false;
while (!done) {
if (in.available()) in.read()
// sleep or wait
}
Then another thread can simply set done = true in order to stop the reading thread.