Duplicate :
|
|
Duplicate :
|
|
Duplicate :
|
If a thread is stuck waiting on some I/O, then there is no clean way to get rid of it quickly. If you close it's file, it won't notice and it will continue to wait on its condition variable. If you kill it using Thread.stop(), it will not receive that exception either since it is waiting on its condition variable. The only way to really kill it is to use Thread.stop() and then wait for it to receive more input from the file which will wake it up from its conditional wait. Here is a test case. The reader thread will never die. import java.io.InputStream; import net.www.html.URL; public class IOTest implements Runnable { InputStream in; public IOTest(InputStream is) { in = is; } public void run() { while (true) { int ch = in.read(); System.out.write(ch); } } public static void main(String argv[]) { IOTest iot = new IOTest(new URL(null, "http://benden:8888/stock.dat/SUNW") .openStream()); Thread t = new Thread(iot); t.start(); Thread.sleep(10000); System.out.print("closing input..."); System.out.flush(); iot.in.close(); System.out.println("done"); Thread.sleep(5000); System.out.print("killing..."); System.out.flush(); t.stop(); System.out.println("done"); } } The description field as copied from bug report 1212384 follows: fp.bugs 1276 - ###@###.### (Jon Payne) Once a thread is blocked in read() it is not possible to kill that thread, or unblock it. Description: I have a piece of applet code which tries to stop another thread which is blocked reading from a network connection. It tries to unblock it by closing the network connection, hoping that the read will return eof immediately. Instead it sits there forever blocked, the thread in PendingDeath state.
|