0
点赞
收藏
分享

微信扫一扫

什么叫防御式编程

王传学 2024-06-07 阅读 32

暴力停止方法 stop

package com.alibaba.fescar.core.protocol.test;

public class TestThreadStop {

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(()->{
            for (int i = 0; i < 10000; i++) {
                System.out.println(i);
            }
        });
        thread.start();
        // 保证子线程thread进入运行状态
        Thread.sleep(10);

        thread.stop();
    }
}

输出结果


周期性检查条件变量

package com.alibaba.fescar.core.protocol.test;

public class TestThreadStop {

    static class MyThread extends Thread {
        //条件变量
        private volatile boolean stop = false;
        private int i = 0;

        public void end() {
            stop = true;
        }

        @Override
        public void run() {
            //循环检查条件变量
            while (!stop) {
                i++;
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread thread = new MyThread();
        thread.start();
        // 保证子线程thread进入运行状态
        Thread.sleep(10);

        thread.end();
    }
}

 输出结果


条件变量结合中断 

package com.alibaba.fescar.core.protocol.test;

public class TestThreadStop {

    static class MyThread extends Thread {
        //条件变量
        private volatile boolean stop = false;
        private int i = 0;

        public void end() {
            stop = true;
            this.interrupt();
        }

        @Override
        public void run() {
            //循环检查条件变量
            while (!stop) {
                //业务代码
                i++;
                System.out.println(i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println("线程被中断");
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread thread = new MyThread();
        thread.start();
        // 保证子线程thread进入运行状态
        Thread.sleep(10);
        thread.end();
    }
}

输出结果


 使用Thread.isInterrupted()来代替条件变量

package com.alibaba.fescar.core.protocol.test;

public class TestThreadStop {

    static class MyThread extends Thread {

        private int i = 0;

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                i++;
                System.out.println(i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println("线程被中断");
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread thread = new MyThread();
        thread.start();
        // 保证子线程thread进入运行状态
        Thread.sleep(1000);

        thread.interrupt();
    }
}

输出结果 

package com.alibaba.fescar.core.protocol.test;

public class TestThreadStop {

    static class MyThread extends Thread {

        private int i = 0;

        @Override
        public void run() {
            while (!Thread.currentThread().isInterrupted()) {
                i++;
                System.out.println(i);
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    System.out.println("线程被中断");
                    e.printStackTrace();
                    // 再次打断
                    this.interrupt();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread thread = new MyThread();
        thread.start();
        // 保证子线程thread进入运行状态
        Thread.sleep(1000);

        thread.interrupt();
    }
}

 输出结果


举报

相关推荐

0 条评论