一个线程可见性的例子

版权声明: https://blog.csdn.net/Dongguabai/article/details/85999187
package test.demo2;

/**
 * @author Dongguabai
 * @date 2019/1/7 13:21
 */
public class ThreadDemo extends Thread {

    private  Boolean stop = false;

    public static void main(String[] args) throws InterruptedException {
        ThreadDemo t = new ThreadDemo();
        t.start();
        Thread.sleep(1000);
        t.stopMe();
    }

    @Override
    public void run() {
        int i = 0;
        while (!stop) {
            i++;
        }
        System.out.println(i);
    }

    public void stopMe() {
        System.out.println(" STOP--------");
        this.stop = true;
    }
}

猜你喜欢

转载自blog.csdn.net/Dongguabai/article/details/85999187
今日推荐