zoukankan      html  css  js  c++  java
  • Java多线程之阻塞I/O如何中断

    阻塞的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();
        }
    
    }
  • 相关阅读:
    深入理解JavaScript系列(24):JavaScript与DOM(下)
    大叔手记(20):ASP.NET MVC中使用jQuery时的浏览器缓存问题
    深入理解JavaScript系列(22):S.O.L.I.D五大原则之依赖倒置原则DIP
    深入理解JavaScript系列(21):S.O.L.I.D五大原则之接口隔离原则ISP
    深入理解JavaScript系列(23):JavaScript与DOM(上)——也适用于新手
    深入理解JavaScript系列(25):设计模式之单例模式
    深入理解JavaScript系列(26):设计模式之构造函数模式
    ASP.NET GridView中使用搜索框(SearchableGridView)
    WPF中的图表设计器 – 1
    使用URL Routing进行数据分页
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3756151.html
Copyright © 2011-2022 走看看