In this chapter we discuss asynchronous phenomena and dirty reads caused by synchronous synchronized (NewObject ()).
1. Code Listing
Package com.ray.deepintothread.ch02.topic_14;/** * * @author Raylee * */public class Asynchofsynchwithunsameobject {Publ IC 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;} @Overridepublic void Run () {try {Myservice.updatea ();} catch (Interruptedexception e) {e.printstacktrace ()}}} Class Threadtwo implements Runnable {private MyService myservice;public threadtwo (MyService myservice) {This.myservice = MyService;} @Overridepublic 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 (+); 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 (+); Long endTime = System.currenttimemillis (); System.out.println ("Updateb endTime:" + EndTime);}}}
Output:
UpdateA starttime:1462628803229
Updateb starttime:1462628803229
Updateb endtime:1462628804229
UpdateA endtime:1462628804229
2. Conclusion
From the output can be seen, UpdateA and updateb basic simultaneous execution, and then at the same time, although both methods are synchronized with the corresponding code block, but because of different threads simultaneously method two methods, and two methods is different, but the method of some statements synchronization, which caused the above phenomenon.
3. Thinking
There is a result of the above asynchronous inference, although the above method is synchronized with some code, but there will still be dirty read the situation.
4. Proof
Package com.ray.deepintothread.ch02.topic_14;/** * * @author Raylee * */public class Dirtyread {public static void main (S Tring[] 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;} @Overridepublic void Run () {try {Myservice.updatea ();} catch (Interruptedexception e) {e.printstacktrace ()}}} Class Threadfour implements Runnable {private MyService2 myservice;public threadfour (MyService2 myservice) { This.myservice = MyService;} @Overridepublic 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.prin TLN (Thread.CurrentThread (). GetName () + "" + id++); Thread.Sleep (50);}} public void Updateb () throws Interruptedexception {synchronized (object2) {for (int i = 0; i < 5; i++) {System.out.prin TLN (Thread.CurrentThread (). GetName () + "" + id++); Thread.Sleep (100);}}}
Output:
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
From the output can be seen, if it is really synchronous, should each thread is the book Order of the downward increase, but the above is obviously not, and in the id=2 position thread-1 appeared dirty read.
Summary: This chapter focuses on asynchronous phenomena and dirty reads caused by synchronized synchronized (NewObject ()), as well as the process of proving.
This chapter is here, thank you.
------------------------------------------------------------------------------------
My github:https://github.com/raylee2015/deepintothread.
Catalog: http://blog.csdn.net/raylee2007/article/details/51204573
Understanding Multithreading-2.13 asynchronous phenomena and dirty reads caused by synchronous synchronized (NewObject ()) from the beginning