zoukankan      html  css  js  c++  java
  • Java多线程之线程中断

    该例子说明,Sleep可以被中断,但是I/O和synchronized不能被中断。

    package Thread.Interrupting;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.Future;
    import java.util.concurrent.TimeUnit;
    
    class SleepBlocked implements Runnable {
        @Override
        public void run() {
            try {
                TimeUnit.SECONDS.sleep(100);
            } catch (Exception e) {
                System.out.println("InterruptedException");
            }
            System.out.println("Exiting SleepBlocked.run()");
        }
    }
    
    class IOBlocked implements Runnable {
        private InputStream in;
    
        public IOBlocked(InputStream is) {
            in = is;
        }
    
        @Override
        public void run() {
            try {
                System.out.println("Waiting for read():");
                in.read();
            } catch (IOException e) {
                //判断线程是否被打断了
                if (Thread.currentThread().isInterrupted()) {
                    System.out.println("Interrupted from blocked I/O");
                } else {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("Exiting IOBlocked.run()");
        }
    }
    
    class SynchronizedBlocked implements Runnable {
        public synchronized void f() {
            while (true)
                Thread.yield();
        }
    
        public SynchronizedBlocked() {
            new Thread() {
                public void run() {
                    f();
                }
            }.start();
        }
    
        @Override
        public void run() {
            System.out.println("Trying to call f()");
            f();//试图中断已经处于锁定的代码
            System.out.println("Exiting SynchronizedBlocked.run()");
        }
    }
    
    public class Interrupting {
        private static ExecutorService exec = Executors.newCachedThreadPool();
    
        static void test(Runnable r) throws InterruptedException {
            Future<?> f = exec.submit(r);
            TimeUnit.MILLISECONDS.sleep(1000);
            System.out.println("Interrupting " + r.getClass().getName());
            f.cancel(true);
            System.out.println("Interrupt sent to " + r.getClass().getName());
        }
    
        public static void main(String[] args) throws InterruptedException {
       test(new SleepBlocked());

        test(new IOBlocked(System.in));
        test(new SynchronizedBlocked());

            TimeUnit.SECONDS.sleep(3);

            System.out.println("Aborting with System.exit(0)");

          System.exit(0);
        }
    }
  • 相关阅读:
    微信小程序支付接口之Django后台
    wx.request 请求与django
    ubuntu16.04 安装使用meld及问题
    微信小程序上传单张或多张图片
    ip地址掩码和位数对应关系表、子网掩码、网络地址、主机地址-yellowcong
    公网IP地址就一定是A类地址和B类地址吗?那C类地址就一定是私有地址吗?
    太厉害了,终于有人能把TCP/IP协议讲的明明白白了!
    linux/shell/bash 自动输入密码或文本
    shell case例子
    spring 配置Value常量(不支持到static上)
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3756057.html
Copyright © 2011-2022 走看看