volatile 验证 java

package interview;

public class VolatileTest implements Runnable {
	private volatile boolean isRun = true;
	
	@Override
	public void run() {
		
		  System.out.println("run start");
	        while(isRun){

	        }
	        System.out.println("run end");
	}
	
	public void setRun(boolean isRun) {
        this.isRun = isRun;
    }
	
	public static void main(String[] args) throws Exception {
		VolatileTest vt = new VolatileTest();
		new Thread(vt).start();
		Thread.sleep(2000);
		vt.setRun(false);
	}

	

}

 

posted @ 2018-07-28 15:47  PyJava老鸟  阅读(254)  评论(1编辑  收藏  举报