【JAVA-线程】volatile

在这里插入图片描述

代码:

//保证数据的同步,只保证可见性
public class VolatileTest {
	private volatile static int num=0;
	public static void main(String[] args) throws InterruptedException {
		new Thread(()->{
			while(num==0) {
				System.out.println("我是while循环");
			}
		}).start();
		Thread.sleep(1);
		num=1;
	}
}

猜你喜欢

转载自blog.csdn.net/beauman/article/details/90176252
今日推荐