阻塞的I/O線程在關閉線程時並不會被打斷,需要關閉資源才能打斷。
1.執行socketInput.close();阻塞可中斷。
2.執行System.in.close();阻塞沒有中斷。
package Thread.Interrupting; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; public class CloseResource { public static void main(String[] args) throws Exception { //堵塞的I/O線程不會被打斷,需要關閉資源才能打斷 ExecutorService exec = Executors.newCachedThreadPool(); ServerSocket server = new ServerSocket(8080); InputStream socketInput = new Socket("localhost", 8080) .getInputStream(); exec.execute(new IOBlocked(socketInput)); exec.execute(new IOBlocked(System.in)); TimeUnit.MILLISECONDS.sleep(100); System.out.println("Shutting down all threads"); exec.shutdownNow(); TimeUnit.SECONDS.sleep(1); System.out.println("Closing " + socketInput.getClass().getName()); socketInput.close(); TimeUnit.SECONDS.sleep(1); System.out.println("Close " + System.in.getClass().getName()); System.in.close(); } }
被阻塞的nio通道在關閉線程后會自動響應中斷阻塞,不需要關閉底層資源。
package Thread.Interrupting; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.nio.ByteBuffer; import java.nio.channels.AsynchronousCloseException; import java.nio.channels.ClosedByInterruptException; import java.nio.channels.SocketChannel; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; class NIOBlocked implements Runnable { private final SocketChannel sc; public NIOBlocked(SocketChannel sc) { this.sc = sc; } @Override public void run() { try { System.out.println("Waiting for read() in " + this); sc.read(ByteBuffer.allocate(1)); } catch (ClosedByInterruptException e) { System.out.println("ClosedByInterruptException"); } catch (AsynchronousCloseException e) { System.out.println("AsynchronousCloseException"); } catch (IOException e) { throw new RuntimeException(e); } System.out.println("Exiting NIOBlocked.run() " + this); } } public class NIOInterruption { public static void main(String[] args) throws Exception { //被阻塞的nio通道會自動地響應中斷 ExecutorService exec = Executors.newCachedThreadPool(); ServerSocket server = new ServerSocket(8080); InetSocketAddress isa = new InetSocketAddress("localhost", 8080); SocketChannel sc1 = SocketChannel.open(isa); SocketChannel sc2 = SocketChannel.open(isa); Future<?> f = exec.submit(new NIOBlocked(sc1)); exec.execute(new NIOBlocked(sc2)); exec.shutdown(); TimeUnit.SECONDS.sleep(1); f.cancel(true); TimeUnit.SECONDS.sleep(1); sc2.close(); } }