The usage of Synchronized keyword modification method in Java _java

Source: Internet
Author: User

Java's most basic way of synchronizing is to use the Synchronized keyword to control concurrent access to a method. Each method declared with the Synchronized keyword is a critical section. In Java, the critical section of the same object is only allowed to be accessed at the same time.

Static methods have different behavior. A static method declared with the Synchronized keyword can only be accessed by one thread of execution, but other threads can access the Non-static synchronized method of the object. This must be very cautious, because two threads can simultaneously access two different synchronized methods of an object, i.e. one is a static synchronized method and the other is a non-static synchronized method. If all two methods change the same data, there will be errors that are inconsistent with the data.

The syntax for the synchronized block is as follows:

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

There are two uses for the Synchronized keyword, one for the definition of the method only, the other for the synchronized block, and we can not only use synchronized to synchronize an object variable, You can also pass Synchronizedl to synchronize static and Non-static methods in a class.

First: Synchronization of Non-static methods
you can tell from Java-related syntax that using the SYNCHRONIZED keyword to define a method locks the static and Non-static methods used in the class using the SYNCHRONIEZD keyword, but that's a little hard to understand, if you want to synchronized blocks, To achieve this effect, it is not difficult to understand why this is the effect, if you use synchronized to lock all synchronous non-static methods in the class, just use this as an argument to the synchronized block to pass in the synchronized block, the code is as follows:

public class Test 
{public 
 void method1 () 
 { 
  synchronized (.) 
   { 
 
   } 
 } 
 
 Public synchronized void Method2 () 
 { 
 
 } 
} public 

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

In the above code, the METHOD1 uses the synchronized block, the Method2 method uses the Synchronized keyword to define the method, and if the same test instance is used, the two methods are executed. Other methods will be blocked because they do not have a synchronized lock. In addition to using this as a parameter to the synchronized block, Test.this can be used as a parameter to the synchronized block to achieve the same effect.


In the synchronized block in the inner class, this only represents the inner class, and the outer class (Outerclass) is not related. However, non-static methods in the inner class and non-static methods of the outer classes can also be synchronized. If you add a method method3 to the inner class, you can also synchronize the 2 methods in test with the following code:

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

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

The Method3 method above Innerclass and the Method1 and Method2 methods of test can only be executed at one time in a single method.
Synchronized block whether the correct execution, or because the program error exit synchronized block, the current synchronized block of the synchronized lock will automatically release, so in the use of synchronized block do not worry about the problem of synchronous lock.

Ii. synchronization of static methods
because object instances are not necessarily created when a static method is invoked, you cannot use this to synchronize static methods, and you must use a class object to synchronize static methods. The code is as follows:

public class test{ 
 
 pubic static void Method1 () { 
  synchronized (test.class) { 
  } 
 } is public 
 static synchronized void Method2 () { 
 
  } 
} public 

class test{ 
 
 pubic static void Method1 () { 
  Synchronized (Test.class) { 
  } 
 } public 
 static synchronized void Method2 () { 
 
  } 
} 

You can use the class's static field class to get the class object when synchronizing static methods, in the example where the Method1 and Method2 methods have only one method to execute, except that you can get class objects using the Class field, and you can also use the instance's GetClass () Method gets the class object with the following code:

public class test{public 
 static test test; 
 Public Test () { 
 test=this; 
 } 
 public static void Method1 () { 
 synchronized (Test.getclass ()) {}} The 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 public static object and get a class object from the GetClass method of the instance (note that all instances of a class are the same class object through the GetClass method). We can also synchronize static methods of different classes by class, with the following code:

public class test1{public 
 static void Method1 () { 
 synchronized (test.class) { 
  } 
 } 
} 

public class test1{public 
 static void Method1 () { 
 synchronized (test.class) { 
  } 
 } 
} 


Note: When you use the synchronized block to synchronize a method, Non-static methods can be synchronized through this, while static methods must be synchronized using Class objects, but Non-static methods can also synchronize static methods by using class. However, you cannot use this in a static method to synchronize a non-static method. This should be noted in the use of synchronized blocks.

Note
The Synchronized keyword lowers the performance of your application, so you can only use it in concurrent scenarios where you need to modify the shared data. If multiple threads access the same synchronized method, only one thread can access it, and other threads will wait. If the method declaration does not use the Synchronized keyword, all threads can execute this method at the same time, and thus the total elapsed time is reduced. If a method is known to not be called by a thread, it is not necessary to use the Synchronized keyword declaration.

You can recursively invoke the method that is declared by the synchronized. When a thread accesses the synchronization method of an object, it can also call the other synchronization methods of the object, as well as the method being executed, without having to gain access to the method again.

We can protect the access of blocks of code (not the entire method) by synchronized keywords. This should take advantage of the Synchronized keyword: The remainder of the method is kept outside the synchronized code block for better performance. Access to a critical section (that is, a block of code that can only be accessed by one thread at a time) should be as short as possible. For example, in the operation of acquiring a building, we only use the Synchronized keyword to protect the number of updated instructions and let other operations not use shared data. When you use the Synchronized keyword this way, you must refer to the object reference as an incoming parameter. Only one thread at a time is allowed to access this synchronized code. In general, we use the This keyword to refer to the object to which the executing method belongs:

Synchronized (this) {
  //java code
}

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.