In this chapter we discuss the similarities between synchronized holding object locks and class locks.
1. When all methods do not use synchronization
Code Listing
Package Com.ray.deepintothread.ch02.topic_2;public class SynchInstance1 {public static void main (string[] args) throws interruptedexception {Mytestobjectone Mytestobjectone = new Mytestobjectone (); for (int i = 0; i < 5; i++) {Threadone th Readtwo = new Threadone (mytestobjectone); Thread thread = new Thread (threadtwo), Thread.setname ("" + i); Thread.Start ();}}} Class Threadone implements Runnable {private Mytestobjectone mytestobjectone;public Threadone (mytestobjectone Mytestobjectone) {this.mytestobjectone = Mytestobjectone;} @Overridepublic void Run () {try {if (Integer.parseint (Thread.CurrentThread (). GetName ())% 2 = = 0) {Mytestobjectone.test1 ();} else {mytestobjectone.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjectone {public void Test1 () throws Interruptedexception {System.out.println (). GetName () + "method test1 begin"); System.out.println (Thread.CurrentThread (). GetName () + "method test1 Waiting"); Thread.Sleep (200); System.out.pRintln (Thread.CurrentThread (). GetName () + "method Test1 End");} public void Test2 () throws Interruptedexception {System.out.println (Thread.CurrentThread (). GetName () + "method test2 Begin "); System.out.println (Thread.CurrentThread (). GetName () + "method test2 waiting"); Thread.Sleep (200); System.out.println (Thread.CurrentThread (). GetName () + "method Test2 End");}}
Output:
0 method Test1 Begin
0 Method Test1 Waiting
4 Method Test1 Begin
4 Method Test1 Waiting
2 Method Test1 begin
2 Method Test1 Waiting
1 Method Test2 begin
1 Method Test2 Waiting
3 Method Test2 begin
3 Method Test2 Waiting
0 Method Test1 End
4 Method Test1 End
2 Method Test1 End
1 Method Test2 End
3 Method Test2 End
The case of holding a class lock:
Package Com.ray.deepintothread.ch02.topic_3;public class SynchInstance1 {public static void main (string[] args) throws interruptedexception {for (int i = 0; i < 5; i++) {Threadone threadtwo = new Threadone (); Thread thread = new Thread (threadtwo), Thread.setname ("" + i); Thread.Start ();}}} Class Threadone implements Runnable {@Overridepublic void run () {try {if (Integer.parseint (Thread.CurrentThread ()). GetName ())% 2 = = 0) {mytestobjectone.test1 ();} else {mytestobjectone.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjectone {public static void Test1 () throws Interruptedexception {System.out.println (thread.currentthread (). GetName () + "method test1 begin"); System.out.println (Thread.CurrentThread (). GetName () + "method test1 Waiting"); Thread.Sleep (200); System.out.println (Thread.CurrentThread (). GetName () + "method Test1 End");} public static void Test2 () throws Interruptedexception {System.out.println (Thread.CurrentThread (). GetName () + "method Test2 Begin "); System.out.println (Thread.CurrentThread (). GetName () + "method test2 waiting"); Thread.Sleep (200); System.out.println (Thread.CurrentThread (). GetName () + "method Test2 End");}}
Output:
0 method Test1 Begin
0 Method Test1 Waiting
2 Method Test1 begin
2 Method Test1 Waiting
3 Method Test2 begin
3 Method Test2 Waiting
1 Method Test2 begin
1 Method Test2 Waiting
4 Method Test1 Begin
4 Method Test1 Waiting
2 Method Test1 End
3 Method Test2 End
0 Method Test1 End
1 Method Test2 End
4 Method Test1 End
From the two outputs can be seen, the method is to cross the execution, there is no synchronization in the inside of the
2. When all methods are used for synchronization
Code Listing
Package Com.ray.deepintothread.ch02.topic_2;public class SynchInstance2 {public static void main (string[] args) throws interruptedexception {mytestobjecttwo mytestobjecttwo = new Mytestobjecttwo (); for (int i = 0; i < 5; i++) {threadtwo th Readtwo = new Threadtwo (mytestobjecttwo); Thread thread = new Thread (threadtwo), Thread.setname ("" + i); Thread.Start ();}}} Class Threadtwo implements Runnable {private mytestobjecttwo mytestobjecttwo;public threadtwo (mytestobjecttwo Mytestobjecttwo) {this.mytestobjecttwo = Mytestobjecttwo;} @Overridepublic void Run () {try {if (Integer.parseint (Thread.CurrentThread (). GetName ())% 2 = = 0) {Mytestobjecttwo.test1 ();} else {mytestobjecttwo.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjecttwo {public synchronized void Test1 () throws Interruptedexception {System.out.println ( Thread.CurrentThread (). GetName () + "method test1 begin"); System.out.println (Thread.CurrentThread (). GetName () + "method test1 Waiting"); System.out.printlN (Thread.CurrentThread (). GetName () + "method Test1 End");} Public synchronized void Test2 () throws Interruptedexception {System.out.println (Thread.CurrentThread (). GetName () + " Method Test2 begin "); System.out.println (Thread.CurrentThread (). GetName () + "method test2 waiting"); System.out.println (Thread.CurrentThread (). GetName () + "method Test2 End");}}
Output:
1 Method Test2 begin
1 Method Test2 Waiting
1 Method Test2 End
0 method Test1 Begin
0 Method Test1 Waiting
0 Method Test1 End
2 Method Test1 begin
2 Method Test1 Waiting
2 Method Test1 End
4 Method Test1 Begin
4 Method Test1 Waiting
4 Method Test1 End
3 Method Test2 begin
3 Method Test2 Waiting
3 Method Test2 End
Package Com.ray.deepintothread.ch02.topic_3;public class SynchInstance2 {public static void main (string[] args) throws interruptedexception {for (int i = 0; i < 5; i++) {Threadtwo threadtwo = new Threadtwo (); Thread thread = new Thread (threadtwo), Thread.setname ("" + i); Thread.Start ();}}} Class Threadtwo implements Runnable {@Overridepublic void run () {try {if (Integer.parseint (Thread.CurrentThread ()). GetName ())% 2 = = 0) {mytestobjecttwo.test1 ();} else {mytestobjecttwo.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjecttwo {public static synchronized void Test1 () throws Interruptedexception {System.out.println ( Thread.CurrentThread (). GetName () + "method test1 begin"); System.out.println (Thread.CurrentThread (). GetName () + "method test1 Waiting"); System.out.println (Thread.CurrentThread (). GetName () + "method Test1 End");} public static synchronized void Test2 () throws Interruptedexception {System.out.println (). GetName () + "method test2 Begin "); System.out.println (Thread.CurrentThread (). GetName () + "method test2 waiting"); System.out.println (Thread.CurrentThread (). GetName () + "method Test2 End");}}
Output:
0 method Test1 Begin
0 Method Test1 Waiting
0 Method Test1 End
1 Method Test2 begin
1 Method Test2 Waiting
1 Method Test2 End
3 Method Test2 begin
3 Method Test2 Waiting
3 Method Test2 End
2 Method Test1 begin
2 Method Test1 Waiting
2 Method Test1 End
4 Method Test1 Begin
4 Method Test1 Waiting
4 Method Test1 End
From the output and debug discovery, each method is executed in sequence, synchronizing the effect
But strangely enough, every time the whole method executes, it's the next method.
According to the usual execution, should be like above, cross the execution, because we use multithreading and is to access different methods, if the synchronization is the method level, you should be able to imagine thread 1 access to test1, I thread 2 can also access test2, if not, So synchronized holds locks that are not method-level, but object-level or class-level, whereas the General class level is for static methods and code blocks.
3. Features
(1) All synchronization methods can only be executed sequentially, the above example shows
(2) Synchronized only the method of the tag, not marked or not synchronized
Package Com.ray.deepintothread.ch02.topic_2;public class SynchInstance3 {public static void main (string[] args) throws interruptedexception {Mytestobjectthree mytestobjectthree = new Mytestobjectthree (); for (int i = 0; i < 5; i++) {Thread Three threadthree = new Threadthree (mytestobjectthree); Thread thread = new Thread (threadthree), Thread.setname ("" + i); Thread.Start ();}}} Class Threadthree implements Runnable {private Mytestobjectthree mytestobjectthree;public threadthree ( Mytestobjectthree mytestobjectthree) {this.mytestobjectthree = Mytestobjectthree;} @Overridepublic void Run () {try {if (Integer.parseint (Thread.CurrentThread (). GetName ())% 2 = = 0) { Mytestobjectthree.test1 ();} else {mytestobjectthree.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjectthree {public synchronized void Test1 () throws Interruptedexception {System.out.println ( Thread.CurrentThread (). GetName () + "method test1 begin"); System.out.println (Thread.CurrentThread (). GetName () + "MeThod test1 Waiting "); System.out.println (Thread.CurrentThread (). GetName () + "method Test1 End");} public void Test2 () throws Interruptedexception {System.out.println (Thread.CurrentThread (). GetName () + "method test2 Begin "); System.out.println (Thread.CurrentThread (). GetName () + "method test2 waiting"); System.out.println (Thread.CurrentThread (). GetName () + "method Test2 End");}}
Output:
0 method Test1 Begin
3 Method Test2 begin
3 Method Test2 Waiting
3 Method Test2 End
1 Method Test2 begin
1 Method Test2 Waiting
1 Method Test2 End
0 Method Test1 Waiting
0 Method Test1 End
2 Method Test1 begin
2 Method Test1 Waiting
2 Method Test1 End
4 Method Test1 Begin
4 Method Test1 Waiting
4 Method Test1 End
Package Com.ray.deepintothread.ch02.topic_3;public class SynchInstance3 {public static void main (string[] args) throws interruptedexception {Mytestobjectthree mytestobjectthree = new Mytestobjectthree (); for (int i = 0; i < 5; i++) {Thread Three threadthree = new Threadthree (mytestobjectthree); Thread thread = new Thread (threadthree), Thread.setname ("" + i); Thread.Start ();}}} Class Threadthree implements Runnable {private Mytestobjectthree mytestobjectthree;public threadthree ( Mytestobjectthree mytestobjectthree) {this.mytestobjectthree = Mytestobjectthree;} @Overridepublic void Run () {try {if (Integer.parseint (Thread.CurrentThread (). GetName ())% 2 = = 0) { Mytestobjectthree.test1 ();} else {mytestobjectthree.test2 ();}} catch (Interruptedexception e) {e.printstacktrace ();}}} Class Mytestobjectthree {public synchronized void Test1 () throws Interruptedexception {System.out.println ( Thread.CurrentThread (). GetName () + "method test1 begin"); System.out.println (Thread.CurrentThread (). GetName () + "MeThod test1 Waiting "); System.out.println (Thread.CurrentThread (). GetName () + "method Test1 End");} public void Test2 () throws Interruptedexception {System.out.println (Thread.CurrentThread (). GetName () + "method test2 Begin "); System.out.println (Thread.CurrentThread (). GetName () + "method test2 waiting"); System.out.println (Thread.CurrentThread (). GetName () + "method Test2 End");}}
Output:
0 method Test1 Begin
0 Method Test1 Waiting
0 Method Test1 End
2 Method Test1 begin
2 Method Test1 Waiting
2 Method Test1 End
1 Method Test2 begin
1 Method Test2 Waiting
4 Method Test1 Begin
4 Method Test1 Waiting
4 Method Test1 End
1 Method Test2 End
3 Method Test2 begin
3 Method Test2 Waiting
3 Method Test2 End
Summary: In this chapter we discuss the situation of synchronized holding a 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.2 synchronized holds the same point of object lock and class lock