Understanding multithreading from scratch-2.15 proving that using domains as multithreaded monitors is out of sync

Source: Internet
Author: User

This chapter goes on to the last error in the previous chapter, and we prove that using integer domains as multithreaded monitors is out of sync.

1. Using the same domain as a multithreaded monitor, is not synchronized

Package com.ray.deepintothread.ch02.topic_16;/** * * @author Raylee * */public class Dirtyreadwithsynchblock {public stat IC 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;} @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 Integer id = 0;public void UpdateA () throws INterruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out.println (Thread.CurrentThread (). GetName ( ) + "" + id++); Thread.Sleep (50);}} public void Updateb () throws Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out.println (T Hread.currentthread (). GetName () + "" + id++); Thread.Sleep (100);}}}

Output:

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


From the above output can draw a corresponding conclusion


Another example:

Package com.ray.deepintothread.ch02.topic_16;/** * * @author Raylee * */public class Dirtyreadwithsynchblock {public stat IC void Main (string[] args) throws Interruptedexception {MyService2 MyService = new MyService2 (); Threadthree threadthree = new Threadthree (MyService); Thread thread = new Thread (threadthree); Thread.setname ("Thread A"); Thread.Start (); Threadfour threadfour = new Threadfour (MyService); Thread thread2 = new Thread (threadfour); Thread2.setname ("Thread B"); 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 Integer id = 0;public void UpdateA () throws Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; I + +) {System.out.println ("ID:" + ID); System.out.println (Thread.CurrentThread (). GetName () + "" + id++); Thread.Sleep (50); System.out.println ("-------------------");}} public void Updateb () throws Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out.println (" ID: "+ id"); System.out.println (Thread.CurrentThread (). GetName () + "" + id++); Thread.Sleep (100); System.out.println ("-------------------");}}}

Output:

id:0
Thread A 0
Id:1
Thread B 1
-------------------
Id:2
Thread A 2
-------------------
Id:3
Thread A 3
-------------------
Id:4
Thread B 4
-------------------
Id:5
Thread A 5
-------------------
-------------------
Id:6
Id:6
Thread B 6
Thread A 7
-------------------
-------------------
Id:8
Thread B 8
-------------------
Id:9
Thread B 9
-------------------


From the output of the results, especially see the middle of the id=6, Threada and THREADB calculation results, it is self-evident.


2. Pseudo-synchronized (NewObject ()) Synchronization example

Package com.ray.deepintothread.ch02.topic_16;/** * * @author Raylee * */public class Synchblock {public static void main ( String[] args) throws Interruptedexception {MyService myservice = new MyService (); int id = 1; Threadone Threadone = new Threadone (MyService, id); Thread thread = new Thread (threadone); Thread.Start (); Threadtwo threadtwo = new Threadtwo (MyService, id); Thread thread2 = new Thread (threadtwo); Thread2.start ();}} Class Threadone implements Runnable {private MyService myservice;private int id = 0;public Threadone (MyService myservice, int id) {This.myservice = Myservice;this.id = ID;} @Overridepublic void Run () {try {Myservice.updatea (id);} catch (Interruptedexception e) {e.printstacktrace ()}}} Class Threadtwo implements Runnable {private MyService myservice;private int id = 0;public Threadtwo (MyService myservice, int id) {This.myservice = Myservice;this.id = ID;} @Overridepublic void Run () {try {myservice.updateb (id);} catch (Interruptedexception e) {e.printstacktrace ()}}} Class MyService {public void UpdateA (Integer id) throws Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out.println (Thread.CurrentThread (). GetName () + "" + id++); Thread.Sleep (50);}} public void Updateb (Integer id) throws Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out . println (Thread.CurrentThread (). GetName () + "" + id++); Thread.Sleep (100);}}}

Output:

Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
Thread-1 1
Thread-1 2
Thread-1 3
Thread-1 4
Thread-1 5


It seems that the above results are synchronous, and it seems that we are also synchronizing access and modification of the same domain, but it is not.

Our above steps are as follows:

(1) Assign a value to each thread's ID

(2) Multithreading for access and modification

In fact, we have been accessing and modifying the ID of the thread object, not the ID in main, we will simplify the above code, and then we'll look at


Package com.ray.deepintothread.ch02.topic_16;/** * * @author Raylee * */public class SynchBlock2 {public static void main (string[] args) throws Interruptedexception {MyService3 MyService = new MyService3 (); Threadfive threadfive = new Threadfive (MyService); Thread thread = new Thread (threadfive); Thread.Start (); Threadsix threadsix = new Threadsix (MyService); Thread thread2 = new Thread (threadsix); Thread2.start ();}} Class Threadfive implements Runnable {private MyService3 myservice;private int id = 1;public threadfive (MyService3 Myservi CE) {this.myservice = MyService;} @Overridepublic void Run () {try {Myservice.updatea (id);} catch (Interruptedexception e) {e.printstacktrace ()}}} Class Threadsix implements Runnable {private MyService3 myservice;private int id = 1;public Threadsix (MyService3 myservice ) {this.myservice = MyService;} @Overridepublic void Run () {try {myservice.updateb (id);} catch (Interruptedexception e) {e.printstacktrace ()}}} Class MyService3 {public void UpdateA (Integer id) throWS Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out.println (Thread.CurrentThread (). Get Name () + "" + id++); Thread.Sleep (50);}} public void Updateb (Integer id) throws Interruptedexception {synchronized (ID) {for (int i = 0; i < 5; i++) {System.out . println (Thread.CurrentThread (). GetName () + "" + id++); Thread.Sleep (100);}}}

Output:

Thread-0 1
Thread-0 2
Thread-0 3
Thread-0 4
Thread-0 5
Thread-1 1
Thread-1 2
Thread-1 3
Thread-1 4
Thread-1 5


The output is no different from the above, therefore, it is concluded that the method inside of the change is the ID of each thread object, is two different domains, which of course no dirty read situation.


Summary: This chapter demonstrates that using integer domains as multithreaded monitors is not synchronized.


This chapter is here, thank you.

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

My github:https://github.com/raylee2015/deepintothread.


Catalog: http://blog.csdn.net/raylee2007/article/details/51204573


Understanding multithreading from scratch-2.15 proving that using domains as multithreaded monitors is out of sync

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.