Learn from the beginning multi-threaded -2.3 synchronized Hold object lock and class lock different points

Source: Internet
Author: User

In this chapter we discuss the game, synchronized holds the different points of the object lock and the class lock-two kinds of locks are parallel things, no intersection.

1. Hold object lock or class lock synchronously

Package Com.ray.deepintothread.ch02.topic_3;public class SynchInstance5 {public static void main (string[] args) throws interruptedexception {mytestobjectfive mytestobjectfive = new mytestobjectfive (); for (int i = 0; i < 2; i++) {THREADFIV E threadfive = new threadfive (mytestobjectfive); Thread thread = new Thread (threadfive), Thread.setname ("" + i); Thread.Start ();}}} Class Threadfive implements Runnable {private mytestobjectfive mytestobjectfive;public threadfive (mytestobjectfive mytestobjectfive) {this.mytestobjectfive = mytestobjectfive;} @Overridepublic void Run () {try {if (Integer.parseint (Thread.CurrentThread (). GetName ())% 2 = = 0) { Mytestobjectfive.test1 ();} else {mytestobjectfive.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjectfive {public synchronized void Test1 () throws interruptedexception {for (int i = 0; i < 5; i++) {Syst Em.out.println (i); Thread.Sleep (100);}} Public synchronized void Test2 () throws interruptedexception {for (int i = 0; i< 5; i++) {System.out.println (i); Thread.Sleep (100);}}

Output:

0
1
2
3
4
0
1
2
3
4


Package Com.ray.deepintothread.ch02.topic_3;public class SynchInstance6 {public static void main (string[] args) throws interruptedexception {for (int i = 0; i < 2; i++) {Threadsix threadsix = new Threadsix (); Thread thread = new Thread (threadsix), Thread.setname ("" + i); Thread.Start ();}}} Class Threadsix implements Runnable {@Overridepublic void run () {try {if (Integer.parseint (Thread.CurrentThread ()). GetName ())% 2 = = 0) {mytestobjectsix.test1 ();} else {mytestobjectsix.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}}  Class Mytestobjectsix {public static synchronized void Test1 () throws interruptedexception {for (int i = 0; i < 5; i++) {System.out.println (i); Thread.Sleep (100);}} public static synchronized void Test2 () throws interruptedexception {for (int i = 0; i < 5; i++) {System.out.println (i) ; Thread.Sleep (100);}}

Output:

0
1
2
3
4
0
1
2
3
4

As in the previous chapters, when we hold object locks or class locks synchronously, the output is in order


2. When holding both the object lock and the class lock

Package Com.ray.deepintothread.ch02.topic_3;public class SynchInstance4 {public static void main (string[] args) throws interruptedexception {Mytestobjectfour mytestobjectthree = new Mytestobjectfour (); for (int i = 0; i < 2; i++) {THREADFO ur threadfour = new Threadfour (mytestobjectthree); Thread thread = new Thread (threadfour), Thread.setname ("" + i); Thread.Start ();}}} Class Threadfour implements Runnable {private Mytestobjectfour mytestobjectthree;public threadfour (mytestobjectfour Mytestobjectthree) {this.mytestobjectthree = Mytestobjectthree;} @Overridepublic void Run () {try {if (Integer.parseint (Thread.CurrentThread (). GetName ())% 2 = = 0) { Mytestobjectthree.test1 ();} else {mytestobjectfour.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjectfour {public synchronized void Test1 () throws interruptedexception {for (int i = 0; i < 5; i++) {Syst Em.out.println (i); Thread.Sleep (100);}} public static synchronized void Test2 () throws Interruptedexception {for(int i = 0; i < 5; i++) {System.out.println (i); Thread.Sleep (100);}}

Output:

0
0
1
1
2
2
3
3
4
4


From the output can be seen, the two are like parallel relations, there is no intersection, alternating appearance. But there is order, too.


Summary: This section discusses the differences between the synchronized holding object lock and the class lock


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 multi-threaded -2.3 synchronized Hold object lock and class lock different points

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.