这是一个简单的测试程序。主线程启动多个子线程。我已添加到 main: Runtime.getRuntime().addShutdownHook(hookThread);
当 JVM 进程按下 ^C 时,这个 hookThread 将设置一个全局关闭标志,并调用 interrupt() 到所有子线程。
我希望所有子线程都返回,但是如果它在 HttpURLConnection.getInputStream() 中,interrupt() 对子线程没有任何作用。子线程在超时(30 秒)后返回,这对我来说延迟太大了。
及时中断 HttpURLConnection.getInputStream() 调用的最佳方法是什么?
在调用了 interrupt() 之后,在子线程堆栈的顶部之后:
"Thread-0" prio=10 tid=0x0000000040b0f800 nid=0x3eab runnable [0x000000004211b000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
- locked <0x00000000fd99ac88> (a java.io.BufferedInputStream)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:632)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)
- locked <0x00000000fd997670> (a sun.net.www.protocol.http.HttpURLConnection)
...