java里面的中断列子

例子里面只是java里面对于中断和响应中断的一些demo,对应可以看一下线程池
1、阻塞队里里面 take 响应中断

public class Demo {
    static class A{
        private BlockingQueue blockingQueue = new LinkedBlockingQueue();
        public Object f() throws InterruptedException {
                return blockingQueue.take();
        }
    }
    public static void main(String[] args) {
          Thread t1 = new Thread(new Runnable() {
              @Override
              public void run() {
                  try {
                      new A().f();
                  } catch (InterruptedException e) {
                      System.out.println(new Date() +" 我被中断了");
                      e.printStackTrace();
                  }
              }
          });
          t1.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       t1.interrupt();
        System.out.println(t1.isInterrupted());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(new Date() +" main end");
    }
}

2、测试sleep 响应中断请求

public class Demo01 {
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                    try {
                        Thread.sleep(20000);
                    } catch (InterruptedException e) {
                        System.out.println("222" + Thread.currentThread().isInterrupted());
                        e.printStackTrace();
                    }
                    while (true){}
            }
        });
        t1.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
        System.out.println("111" + t1.isInterrupted());
    }
}

3、测试join方法 响应中断

public class JoinTest {
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                }
            }
        });
        t1.start();

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    t1.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    System.out.println("--------------------");
                }
            }
        });
        t2.start();

        Thread.sleep(2000);
        t2.interrupt();
        Thread.sleep(3000);
        System.out.println("中断状态被重置"+t2.isInterrupted());
    }
}

4、测试wait方法 响应中断

public class Demo03 {
    static class AA{
        public synchronized void f(){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("1213");
        }
    }
    public static void main(String[] args) {
        Demo03.AA aa = new Demo03.AA();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                aa.f();
            }
        });
        t1.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t1.interrupt();
        System.out.println(t1.isInterrupted());
    }
}

5、测试处于锁等待的线程 不能响应中断

public class Demo04 {
   static class AA{
        public synchronized void f(){
            while (true){}
        }
    }
    public static void main(String[] args) {
        AA aa = new AA();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                aa.f();
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                aa.f();
            }
        });
        t1.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
        t2.interrupt();
    }
}

6、测试 lockInterruptibly 锁等待的线程 可以响应中断

public class Demo05 {
    static class AA{
        private ReentrantLock lock = new ReentrantLock();
        public void f(){
            try {
                lock.lockInterruptibly();
                while (true){}
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.unlock();
            }

        }
    }
    public static void main(String[] args) {
        Demo05.AA aa = new Demo05.AA();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                aa.f();
            }
        });
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                aa.f();
            }
        });
        t1.start();
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
        t2.interrupt();
    }
}

7、读取过程中的阻塞 不响应中断 有中文结果是乱码的 不影响测试

public class Demo06 {
    static class AA{
        public void f(){
            FileInputStream in = null;
            try {
                in = new FileInputStream(new File("D:\\com.json"));
                byte b = 0;
                while ((b = (byte) in.read()) != -1){
                    System.out.print((char)b);
                }
                System.out.println("read"+  new Date());
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        Demo06.AA aa = new Demo06.AA();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                aa.f();
            }
        });
        t1.start();
        t1.interrupt();
        System.out.println("------------------------------------"+t1.isInterrupted());
        System.out.println("-------------------------------------main"+  new Date());
    }
}

8、中断之后调用阻塞方法

public class Demo07 {
    static class A{
        private BlockingQueue blockingQueue = new LinkedBlockingQueue();
        public Object f() throws InterruptedException {
            Thread.currentThread().interrupt();
            return blockingQueue.take();
        }
    }
    public static void main(String[] args) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new Demo07.A().f();
                } catch (InterruptedException e) {
                    System.out.println(new Date() +" 我被中断了");
                    e.printStackTrace();
                }
            }
        });
        t1.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
       // t1.interrupt();
    }
}

9、shutdownNow()不会让运行中的任务立即停止

public class Demo09 {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(3);
        es.submit(new Runnable() {
            @Override
            public void run() {
                 while (true){
                     System.out.println("1");
                 }
            }
        });
        es.submit(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("2");
                }
            }
        });
        es.submit(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println("3");
                }
            }
        });
        es.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println("4");
            }
        });
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        List<Runnable>  list = es.shutdownNow();
        System.out.println();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值