一个指令重排序的例子

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

/**
 * @author Dongguabai
 * @date 2019/1/7 13:21
 */
public class SynchronizedDemo {
	// 共享变量
	private boolean flag = false;
	private int a = 1;
	private int result = 0;

	//synchronized
	public synchronized void write(){
		// 这里有可能会发生指令重排
		flag = true;// 1.1
		a = 2; // 1.2
	}

	public synchronized void read(){
		if(flag){// 2.1
			result = a * 3; // 2.2
		}
		System.out.println("result: " + result);
	}
	
	private class ReadWriteThread extends Thread{
		private boolean flag;
		public ReadWriteThread(boolean flag){
			this.flag = flag;
		}
		
		@Override
		public void run() {
			if(flag){
				write();
			}else{
				read();
			}
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		SynchronizedDemo demo = new SynchronizedDemo();
		demo.new ReadWriteThread(true).start();// 写
		Thread.sleep(10);
		demo.new ReadWriteThread(false).start();// 读
		// CPU
	}
}

 

展开阅读全文

没有更多推荐了,返回首页