java synchronized: similarities and differences between synchronized static methods and synchronized non static methods

  • 2020-05-30 20:18:18
  • OfStack

java synchronized,

The synchronized keyword can be used in two ways. One is for method definition only, and the other is for synchronized block. Not only can we use synchronized to synchronize an object variable, but you can also use synchronizedl to synchronize static and non-static methods in a class.

The syntax of the synchronized block is as follows:


public void method() 
{ 
  synchronized( expression ) 
   { 
   } 
 
} 


public void method() 
{ 
  synchronized( expression ) 
   { 
   } 
 
} 

Type 1: synchronization of non-static methods

Can know from Java related grammar use synchronized keywords to define the method will be used in the locking classes use synchroniezd keyword to define static methods and non-static methods, but it's a bit not understand, if you want to synchronized block, in order to achieve such effect, it is not hard to understand why will have this effect, if use synchronized to lock all synchronous non-static methods in the class, only need to use this as the parameter to synchronized synchronized piece of block, the code is as follows:

Synchronize non-static methods through the synchronized block

In the above code, method1 USES the synchronized block, and method2 USES the synchronized keyword to define the method. If the same Test instance is used, as long as one of the two methods is executing, the other methods will be blocked because they do not get the synchronization lock. In addition to using this as a parameter for the synchronized block, you can use Test.this as a parameter for the synchronized block to achieve the same effect.


public class Test 
{ 
 public void method1() 
 { 
  synchronized(this) 
   { 
 
   } 
 } 
 
 public synchronized void method2() 
 { 
 
 } 
} 

public class Test 
{ 
 public void method1() 
 { 
  synchronized(this) 
   { 
 
   } 
 } 
 
 public synchronized void method2() 
 { 
 
 } 
} 

In the synchronized block used in the inner class, this only represents the inner class and has no relation to the outer class (OuterClass). But non-static methods in an inner class and non-static methods in an outer class can also be synchronized. If a method method3 is added to the inner class, it can also synchronize with the two methods in Test. The code is as follows:


public class Test 
{ 
 class InnerClass 
 { 
  public void method3() 
   { 
    synchronized(Test.this){ 
 
    } 
   } 
  } 
} 

public class Test 
{ 
 class InnerClass 
 { 
  public void method3() 
   { 
    synchronized(Test.this){ 
 
    } 
   } 
  } 
} 

The above InnerClass method3 method and Test method1 and method2 methods can only execute one method in the same time.

Whether the synchronized block is executed correctly or exits the synchronized block due to an exception due to a program error, the current synchronized block will automatically release the synchronization lock held by the synchronized block, so there is no need to worry about the synchronization lock when using the synchronized block.

2. Synchronization of static methods

Because object instances are never created when a static method is called, you cannot use this to synchronize static methods, but you must use Class objects to synchronize static methods. The code is as follows:


public class Test{ 
 
 pubic static void method1(){ 
  synchronized(Test.class){ 
  } 
 } 
 public static synchronized void method2(){ 
 
  } 
} 

public class Test{ 
 
 pubic static void method1(){ 
  synchronized(Test.class){ 
  } 
 } 
 public static synchronized void method2(){ 
 
  } 
} 

When synchronizing static methods, the static field class of the class can be used to get class objects. In the above example, method1 and method2 methods have only one method execution. In addition to using the class field to get class objects, class objects can also be obtained through the getClass() method of the instance.


public class Test{ 
 public static Test test; 
 public Test(){ 
 test=this; 
 } 
 public static void method1(){ 
 synchronized(test.getClass()){ 
 } 
 } 
} 

public class Test{ 
 public static Test test; 
 public Test(){ 
 test=this; 
 } 
 public static void method1(){ 
 synchronized(test.getClass()){ 
 } 
 } 
} 

In the code above, we get an instance of Test from a static public object, and we get an class object from the getClass method of this instance (note that all instances of a class get the same Class object from the getClass method). We can also use class to synchronize different classes of static methods. The code is as follows:

Methods in the Test class are synchronized with methods in the Test1 class.


public void method() 
{ 
  synchronized( expression ) 
   { 
   } 
 
} 
0

public void method() 
{ 
  synchronized( expression ) 
   { 
   } 
 
} 
1

Note: when using the synchronized block to synchronize methods, non-static methods can be synchronized with this, and static methods must be synchronized with class objects, but non-static methods can also synchronize static methods using class. However, you cannot use this in static methods to synchronize non-static methods. This should be noted when using the synchronized block.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: