这一章节我们来讨论一下由同步的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