volatile可见性测试demo

package test;

public class VolatileTest {
	
	boolean stop = false;
//	volatile boolean stop = false;
	
	public static void main(String[] args) throws Exception{
		VolatileTest v = new VolatileTest();
		Thread ta = new Thread(()->v.execute());
		ta.start();
		Thread.sleep(2000);
		Thread tb = new Thread(()->v.shutdown());
		tb.start();
	}
	
	public void execute(){
		while(!stop){
			String a = "a";
//			System.out.print("");
		}
	}
	public void shutdown(){
		System.out.println("do stop");
		stop = true;
	}
	
	

}

执行出现死循环,加上volatile后则正常

猜你喜欢

转载自blog.csdn.net/u010653311/article/details/84137842