从头认识多线程-2.14 由同步的synchronized (newobject()) 引起的异步现象和脏读

这一章节我们来讨论一下由同步的synchronized (newobject()) 引起的异步现象和脏读。

1.代码清单

package com.ray.deepintothread.ch02.topic_14;

/**
 * 
 * @author RayLee
 *
 */
public class AsynchOfSynchWithUnSameObject {
	public static void main(String[] args) throws InterruptedException {
		MyService myService = new MyService();
		ThreadOne threadOne = new ThreadOne(myService);
		Thread thread = new Thread(threadOne);
		thread.start();
		ThreadTwo threadTwo = new ThreadTwo(myService);
		Thread thread2 = new Thread(threadTwo);
		thread2.start();
	}
}

class ThreadOne implements Runnable {

	private MyService myService;

	public ThreadOne(MyService myService) {
		this.myService = myService;
	}

	@Override
	public void run() {
		try {
			myService.updateA();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class ThreadTwo implements Runnable {

	private MyService myService;

	public ThreadTwo(MyService myService) {
		this.myService = myService;
	}

	@Override
	public void run() {
		try {
			myService.updateB();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class MyService {

	private Object object1;
	private Object object2;

	public MyService() {
		object1 = new Object();
		object2 = new Object();
	}

	public void updateA() throws InterruptedException {
		synchronized (object1) {
			long startTime = System.currentTimeMillis();
			System.out.println("updateA startTime:" + startTime);
			Thread.sleep(1000);
			long endTime = System.currentTimeMillis();
			System.out.println("updateA endTime:" + endTime);
		}
	}

	public void updateB() throws InterruptedException {
		synchronized (object2) {
			long startTime = System.currentTimeMillis();
			System.out.println("updateB startTime:" + startTime);
			Thread.sleep(1000);
			long endTime = System.currentTimeMillis();
			System.out.println("updateB endTime:" + endTime);
		}
	}

}

输出:

updateA startTime:1462628803229
updateB startTime:1462628803229
updateB endTime:1462628804229
updateA endTime:1462628804229


2.结论

从输出可以看见,updateA和updateB基本同时执行,然后同时结束,虽然这两个方法都是同步了相应的代码块,但是由于不同线程同时方法两个方法,而两个方法之间是不同步,只是方法内的某些语句同步,这就造成了上面的这种现象。


3.思考

有上面异步的结果推断,上面的方法虽然是同步了某些代码,但是还是会出现脏读的情况。


4.证明

package com.ray.deepintothread.ch02.topic_14;

/**
 * 
 * @author RayLee
 *
 */
public class DirtyRead {
	public static void main(String[] args) throws InterruptedException {
		MyService2 myService = new MyService2();
		ThreadThree threadThree = new ThreadThree(myService);
		Thread thread = new Thread(threadThree);
		thread.start();
		ThreadFour threadFour = new ThreadFour(myService);
		Thread thread2 = new Thread(threadFour);
		thread2.start();
	}
}

class ThreadThree implements Runnable {

	private MyService2 myService;

	public ThreadThree(MyService2 myService) {
		this.myService = myService;
	}

	@Override
	public void run() {
		try {
			myService.updateA();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class ThreadFour implements Runnable {

	private MyService2 myService;

	public ThreadFour(MyService2 myService) {
		this.myService = myService;
	}

	@Override
	public void run() {
		try {
			myService.updateB();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}

class MyService2 {

	private Object object1;
	private Object object2;

	private int id = 0;

	public MyService2() {
		object1 = new Object();
		object2 = new Object();
	}

	public void updateA() throws InterruptedException {
		synchronized (object1) {
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " " + id++);
				Thread.sleep(50);
			}
		}
	}

	public void updateB() throws InterruptedException {
		synchronized (object2) {
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " " + id++);
				Thread.sleep(100);
			}
		}
	}

}

输出:

Thread-0 0
Thread-1 1
Thread-0 2
Thread-1 4
Thread-0 3
Thread-0 5
Thread-0 6
Thread-1 6
Thread-1 7
Thread-1 8


从输出可以看见,如果真的是同步,应该每一个Thread都是书序的往下增加,但是上面显然不是,而且在id=2的位置thread-1出现了脏读。



总结:这一章节主要讨论了由同步的synchronized (newobject()) 引起的异步现象和脏读,以及证明的过程。


这一章节就到这里,谢谢

------------------------------------------------------------------------------------

我的github:https://github.com/raylee2015/DeepIntoThread


目录:http://blog.csdn.net/raylee2007/article/details/51204573


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值