Use and Analysis of Java multi-thread synchronous synchronized

Source: Internet
Author: User

Synchronization concept:

Synchronization can be divided into two methods: synchronization method and synchronization block.

The locked content includes a specific instance of the lock class and the locked Class Object (all instances of the class)

Variables include instance variables (without static variables) and class variables (with static variables)

Reason for using Synchronization

1. The access class should be accessed using multiple threads in the system;

2. There are class variables in the class, or there is access to public resources (such as reading and writing an external file) in the class method ).

What is the content locked by the synchronization lock?

Whether you add synchronized to the method or to a variable, the lock is a class object. Each object has only one lock associated with it.

The following example lists the synchronization results in various situations.

1. Add synchronized to the method (synchronous method, locking class instances)

JavaCode

 

 
 
  1. Public ClassDemo1 {
  2. Public Synchronized VoidM1 (){
  3. //............... 
  4. }
  5. Public VoidM2 (){
  6. //............ 
  7. Synchronized(This){
  8. //......... 
  9. }
  10. //........ 
  11. }
  12. }

 

The effects of these two methods are the same, and the lock is the class instance object. If there is a class instance object: Demo = new demo1 () and there are two other threads: thread1 and thread2, both of which call the demo object, then at the same time, if thread1 calls the demo. m1 (), thread2 cannot access the demo in this period. m1 () and demo. m2 (); Because thread1 uses the lock of the demo object, it cannot be used by other threads.

However, if thread1 calls demo1.m1 () and thread2 calls demo2.m1 (), it can be done at the same time because they call different types of demo1 object instances.

2. Add synchronized to the variable (synchronization block, locking class instances)

Java code

 

 
 
  1. Public ClassDemo2 {
  2. Object A =NewObject ();
  3. Object B =NewObject ();
  4. Public VoidM1 (){
  5. //............ 
  6. Synchronized(){
  7. //......... 
  8. }
  9. //........ 
  10. }
  11. Public VoidM2 (){
  12. //............ 
  13. Synchronized(B ){
  14. //......... 
  15. }
  16. //........ 
  17. }
  18. }

 

In this case, the block lock is implemented. The locked object is variable A or variable B. (Note that both A and B are non-static.) If there is a class instance object: demo = new demo2 (). There are two other threads: thread1 and thread2, both of which call the demo object. At the same time, if thread1 calls the demo. m1 (), then thread2 can access the demo in this period. m2 (); but cannot access demo. m1 () Synchronization block because a is locked by thread1.

3. Synchronized locks the class variable, that is, the static variable (which may be an attribute or a method) (which locks the class object)

Java code

 

 
 
  1. Public ClassDemo3 {
  2. StaticObject o =NewObject ();
  3. Public Static Synchronized VoidM1 (){
  4. //.... 
  5. }
  6. Public Static VoidM2 (){
  7. //... 
  8. Synchronized(Demo3.Class){
  9. //..... 
  10. }
  11. //..... 
  12. }
  13. Public Static VoidM3 (){
  14. //.......... 
  15. Try{
  16. Synchronized(Class. forname ("Demo3")){
  17. //............ 
  18. }
  19. }Catch(Classnotfoundexception ex ){
  20. }
  21. //............. 
  22. }
  23. Public Static VoidM4 (){
  24. //............ 
  25. Synchronized(O ){
  26. //........ 
  27. }
  28. //.......... 
  29. }
  30. }

 

The effects of the above four methods are the same. The locked objects are class demo3 rather than class instance objects. That is, in multithreading, the shared resources belong to the class, instead of class objects. In this case, if thread1 accesses any of the four methods, other threads cannot access these four methods at the same time.

4. The class method accesses resources shared by multiple threads and the resource is variable. In this case, synchronization is also required.

Java code

 

 
 
  1. Public ClassDemo4 {
  2. StaticString Path ="File path";
  3. Public VoidReadconsponile (){
  4. Synchronized(PATH ){
  5. // Read the file specified by this path. 
  6. }
  7. }
  8. Public VoidWriteconsponile (){
  9. Synchronized(PATH ){
  10. // Write information to the file specified by the path. 
  11. }
  12. }
  13. }

 

In this case, you must lock it as a class variable instead of locking the class instance object, because this is a variant of resource sharing, rather than class instance object resource sharing.

It can improve the performance if it is used well. If it is used well, the system will suffer endless troubles.

PS: thread synchronization requires a lot of system overhead. Therefore, if not necessary, do not use the synchronization function.

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.