zoukankan      html  css  js  c++  java
  • 线程中断interrupt

    public class InterruptThread2 extends Thread{
    
        public static void main(String[] args) {
            try {
                InterruptThread2 t = new InterruptThread2();
                t.start();
                Thread.sleep(1000);
                t.interrupt();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public void run() {
            super.run();
            for(int i = 0; i <= 800000; i++) {//设置中断标识,线程没有影响,还会继续运行。线程只有在方法执行完了,才会退出,
                System.out.println("i=" + i);
            }
        }
        
        public void run1() {
            super.run();
            for(int i = 0; i <= 200000; i++) {
                //判断是否被中断
                if(Thread.currentThread().isInterrupted()){
                    //设置中断标识,线程没有影响,还会继续运行。这里要线程的方法执行完,线程就退出了。
                    //处理中断逻辑
                    for(int j = 0;j < 10 ;j++) {
                        System.out.println("sssss" + j);
                    }
                    break;
                }
                System.out.println("i=" + i);
            }
        }
    }
    public class InterruptThread1 extends Thread{
    
        public static void main(String[] args) {
            try {
                InterruptThread1 t = new InterruptThread1();
                t.start();
                Thread.sleep(1000);
                t.stop();//停止不了线程
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    
        public void run() {
            super.run();
            for(int i = 0; i <= 800000; i++) {//设置中断标识,线程没有影响,还会继续运行。线程只有在方法执行完了,才会退出,
                try {
                    Thread.currentThread().sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("i=" + i);
            }
        }
    }
  • 相关阅读:
    TCP通信丢包原因总结
    根据日志查看QPS
    mysql:备份、复制
    集群
    redis性能提升
    redis源码——多机数据库的实现
    redis源码——单机数据库的实现
    redis 设置过期Key 的 maxmemory-policy 六种方式
    字符处理
    贝塞尔曲线
  • 原文地址:https://www.cnblogs.com/yaowen/p/13496466.html
Copyright © 2011-2022 走看看