Learn from the beginning multithreading-2.24 modifying the Monitor's effect on synchronization

Source: Internet
Author: User

In this chapter we will discuss the effect of modifying the monitor on synchronization.

1. Do not modify the monitor, sync the

Package com.ray.deepintothread.ch02.topic_24;/** * * @author Raylee * */public class Synchofstaticinnerclass {public stat IC void Main (string[] args) {thread thread = new Thread (new Runnable () {public void run () {Myservice.updatea ()}}); Thread.Start (); Thread thread2 = new Thread (new Runnable () {public void run () {myservice.updateb ()}); Thread2.start ();}} Class MyService {private static int id = 0;private static String lock = "A";p ublic static void UpdateA () {Synchronized (LO CK) {for (int i = 0; i < i++) {System.out.println ("Thread name:" + Thread.CurrentThread (). GetName () + "ID:" + id++ try {thread.sleep;} catch (Interruptedexception e) {e.printstacktrace ()}}} public static void Updateb () {synchronized (lock) {for (int i = 0; i <; i++) {System.out.println ("Thread name:" + Th Read.currentthread (). GetName () + "ID:" + id++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}}

Output:

Thread name:thread-0 id:0
Thread name:thread-0 id:1
Thread name:thread-0 Id:2
Thread name:thread-0 Id:3
Thread name:thread-0 Id:4
Thread name:thread-0 Id:5
Thread name:thread-0 Id:6
Thread name:thread-0 Id:7
Thread name:thread-0 Id:8
Thread name:thread-0 Id:9
Thread name:thread-1 Id:10
Thread name:thread-1 id:11
Thread name:thread-1 Id:12
Thread name:thread-1 id:13
Thread name:thread-1 id:14
Thread name:thread-1 id:15
Thread name:thread-1 id:16
Thread name:thread-1 id:17
Thread name:thread-1 id:18
Thread name:thread-1 id:19


2. When the modification occurs, the synchronization becomes asynchronous when the thread starts

Package com.ray.deepintothread.ch02.topic_24;/** * * @author Raylee * */public class SynchOfStaticInnerClass2 {public STA tic void Main (string[] args) {thread thread = new Thread (new Runnable () {public void run () {Myservice2.updatea ()}); Thread.Start (); Thread thread2 = new Thread (new Runnable () {public void run () {myservice2.updateb ()}); Thread2.start ();}} Class MyService2 {private static int a = 0;private static object lock = new Object ();p ublic static void UpdateA () {Synchro Nized (lock) {lock = new Object (); for (int i = 0; i < i++) {System.out.println ("Thread name:" + Thread.currentthrea D (). GetName () + "A:" + a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}} public static void Updateb () {synchronized (lock) {for (int i = 0; i <; i++) {System.out.println ("Thread name:" + Th Read.currentthread (). GetName () + "A:" + a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}}

Output:

Thread name:thread-1 a:0
Thread name:thread-0 a:0
Thread name:thread-1 a:1
Thread name:thread-0 A:2
Thread name:thread-0 A:3
Thread name:thread-1 A:4
Thread name:thread-0 A:5
Thread name:thread-1 A:6
Thread name:thread-0 A:7
Thread name:thread-1 A:8
Thread name:thread-1 A:9
Thread name:thread-0 A:10
Thread name:thread-0 a:11
Thread name:thread-1 A:12
Thread name:thread-1 a:13
Thread name:thread-0 a:14
Thread name:thread-0 a:15
Thread name:thread-1 a:16
Thread name:thread-0 a:17
Thread name:thread-1 a:18


Here the reason is unclear, I have tried in for each execution once on the output of the corresponding lock, are the same, but, from the output is already visible, in fact, it is not synchronized


3. There is no effect on synchronization after a period of time when the thread has started to change

Package com.ray.deepintothread.ch02.topic_24;/** * * @author Raylee * */public class SYNCHOFSTATICINNERCLASS3 {public STA tic void Main (string[] args) {thread thread = new Thread (new Runnable () {public void run () {Myservice3.updatea ()}); Thread.Start (); Thread thread2 = new Thread (new Runnable () {public void run () {myservice3.updateb ()}); Thread2.start ();}} Class MyService3 {private static int a = 0;private static object lock = new Object ();p ublic static void UpdateA () {Synchro Nized (Lock) {for (int i = 0; i <; i++) {if (i = = 5) {//Change the monitor to a delay of lock = new Object ();} System.out.println ("Thread name:" + Thread.CurrentThread (). GetName () + "A:" + a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}} public static void Updateb () {synchronized (lock) {for (int i = 0; i <; i++) {System.out.println ("Thread name:" + Th Read.currentthread (). GetName () + "A:" + a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}}

Output:

Thread name:thread-0 a:0
Thread name:thread-0 a:1
Thread name:thread-0 A:2
Thread name:thread-0 A:3
Thread name:thread-0 A:4
Thread name:thread-0 A:5
Thread name:thread-0 A:6
Thread name:thread-0 A:7
Thread name:thread-0 A:8
Thread name:thread-0 A:9
Thread name:thread-1 A:10
Thread name:thread-1 a:11
Thread name:thread-1 A:12
Thread name:thread-1 a:13
Thread name:thread-1 a:14
Thread name:thread-1 a:15
Thread name:thread-1 a:16
Thread name:thread-1 a:17
Thread name:thread-1 a:18
Thread name:thread-1 a:19


Package com.ray.deepintothread.ch02.topic_24;/** * * @author Raylee * */public class SynchOfStaticInnerClass4 {public STA tic void Main (string[] args) {thread thread = new Thread (new Runnable () {public void run () {try {Myservice4.updatea ();} CA TCH (interruptedexception e) {e.printstacktrace ();}}); Thread.Start (); Thread thread2 = new Thread (new Runnable () {public void run () {myservice4.updateb ()}); Thread2.start ();}} Class MyService4 {private static int a = 0;private static object lock = new Object ();p ublic static void UpdateA () throws I nterruptedexception {synchronized (lock) {thread.sleep (300);//delay the change to the monitor .lock = new Object (), for (int i = 0; i < i++) {System.out.println ("Thread name:" + Thread.CurrentThread (). GetName () + "A:" + a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}} public static void Updateb () {synchronized (lock) {for (int i = 0; i <; i++) {System.out.println ("Thread name:" + Th Read.currentthread (). GetName () + "A:" + a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}}

Output:

Thread name:thread-0 a:0
Thread name:thread-0 a:1
Thread name:thread-0 A:2
Thread name:thread-0 A:3
Thread name:thread-0 A:4
Thread name:thread-0 A:5
Thread name:thread-0 A:6
Thread name:thread-0 A:7
Thread name:thread-0 A:8
Thread name:thread-0 A:9
Thread name:thread-1 A:10
Thread name:thread-1 a:11
Thread name:thread-1 A:12
Thread name:thread-1 a:13
Thread name:thread-1 a:14
Thread name:thread-1 a:15
Thread name:thread-1 a:16
Thread name:thread-1 a:17
Thread name:thread-1 a:18
Thread name:thread-1 a:19


4. Modify the properties of the monitor object without affecting synchronization

Package com.ray.deepintothread.ch02.topic_24;/** * * @author Raylee * */public class SYNCHOFSTATICINNERCLASS5 {public STA tic void Main (string[] args) {MyService5 myService5 = new MyService5 (); Thread thread = new Thread (new Runnable () {public void run () {try {Myservice5.updatea ()} catch (Interruptedexception e) { E.printstacktrace ();}}); Thread.Start (); Thread thread2 = new Thread (new Runnable () {public void run () {myservice5.updateb ()}); Thread2.start ();}}  Class MyService5 {private static int a = 0;private static Monitor lock;public MyService5 () {lock = new monitor (); lock.id = 1;}  public void UpdateA () throws Interruptedexception {synchronized (lock) {lock.id = 2;//Modify the properties of the object Thread.Sleep (+); for (int i = 0; I < 10; i++) {System.out.println ("Thread name:" + Thread.CurrentThread (). GetName () + "A:" + a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}} public void Updateb () {synchronized (lock) {for (int i = 0; i <; i++) {System.out.println ("ThreadName: "+ Thread.CurrentThread (). GetName () +" A: "+ a++); try {thread.sleep (50);} catch (Interruptedexception e) {e.printstacktrace ();}}}} Class Monitor {public int id = 0;}


Output:

Thread name:thread-0 a:0
Thread name:thread-0 a:1
Thread name:thread-0 A:2
Thread name:thread-0 A:3
Thread name:thread-0 A:4
Thread name:thread-0 A:5
Thread name:thread-0 A:6
Thread name:thread-0 A:7
Thread name:thread-0 A:8
Thread name:thread-0 A:9
Thread name:thread-1 A:10
Thread name:thread-1 a:11
Thread name:thread-1 A:12
Thread name:thread-1 a:13
Thread name:thread-1 a:14
Thread name:thread-1 a:15
Thread name:thread-1 a:16
Thread name:thread-1 a:17
Thread name:thread-1 a:18
Thread name:thread-1 a:19


Summary: In this chapter we summarize the effects of several modifications to the monitor on synchronization.


This chapter is here, thank you.

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

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


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


Learn from the beginning multithreading-2.24 modifying the Monitor's effect on synchronization

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.