Interrupt read() operation
I need one thread to interrupt a second thread, but only when the second thread in a read operation:
public void run()// Thread 1
Code
.
.
.
Code
try
{
BIS.read()// BIS is BufferedOutputStream
}
catch (Exception e)
{
}
More Code
.
.
.
More Code
}
Essentially I want Thread 2 to be able to interrupt() Thread 1 (I have already captured Thread 1 to a variable Thread 2 has access), but only if Thread 1 is in the read() portion of the statement. Any suggestions?
Thanks,
Kevin
[628 byte] By [
kcook73] at [2007-9-30 7:47:41]

> Any suggestions?
Yes - in your interruptee thread class (call it class B), have a method "isInIO()" (and its corresponding member) to keep the I/O state. And wrap any I/O calls with private methods that set and unset this flag before and after the I/O.
In thread A, check the state of the flag in thread B before interrupting it.
If this is what you *really* want. Why don't you want to interrupt B if it's *not* in I/O?
AFIAK, if you want to interrupt a thread that is waiting on a read, the only way to do that is to close the thing (socket, file) that is being read from. Or you can use nio.