Home Java javaTutorial An explanation of the basic usage of the synchronized keyword in Java multi-thread programming

An explanation of the basic usage of the synchronized keyword in Java multi-thread programming

Jan 05, 2017 pm 03:48 PM

In multi-threaded programming, the most critical and most concerning issue should be synchronization. This is a difficult point and the core.
From the synchronized and volatile versions of jdk to the Lock interface in the java.util.concurrent.locks package provided in jdk 1.5 (which implements ReadLock, WriteLock, and ReentrantLock), the implementation of multi-threading is also maturing step by step. change.

Synchronization, what mechanism is it controlled through? The first reaction is locks. You should have been exposed to this when learning operating systems and databases. In a Java multi-threaded program, when multiple programs compete for the same resource, in order to prevent resource corruption, an object lock is assigned to the first thread to access the resource, and the later ones need to wait for the release of the object lock.

Yes, Java thread synchronization is most concerned about the use of shared resources.

First let’s understand some shared resources of threads.
Understand which thread-shared data needs to be coordinated from the JVM:
1. Instance variables stored in the heap; 2. Class variables saved in the method area.

When the Java virtual machine loads a class, each object or class will be associated with a monitor to protect the object's instance variables or class variables; of course, if the object has no instance variables or class variables, Without variables, the monitor monitors nothing.

In order to realize the mutual exclusivity of the monitor mentioned above, the virtual machine associates a lock (also called invisible lock) for each object or class. Here, it is explained that class locks are also obtained through object locks. This is achieved because when a class is loaded, the JVM will create an instance of java.lang.Class for each class; therefore, when the object is locked, the class object of this class is also locked.

In addition, a thread can lock an object multiple times, which corresponds to multiple releases; it is a lock calculator provided by the JVM for each object lock. The last lock is added 1, the corresponding decrement is 1, and when the value of the calculator is 0, it is released. This object lock is used by the monitor inside the JVM and is automatically generated by the JVM. All programmers do not need to add it themselves.

After introducing the synchronization principle of Java, we get to the point. Let’s first talk about the use of synchronized, and other synchronization will be introduced in subsequent chapters.

Let’s try running an example first.

package thread_test; 
  
/** 
 * 测试扩展Thread类实现的多线程程序 
 * 
 */
public class TestThread extends Thread{  
  private int threadnum; 
  
  public TestThread(int threadnum) {  
    this.threadnum = threadnum;  
  } 
    
  @Override
  public synchronized void run() {  
    for(int i = 0;i<1000;i++){  
          System.out.println("NO." + threadnum + ":" + i ); 
    } 
    }  
    
    public static void main(String[] args) throws Exception {  
      for(int i=0; i<10; i++){ 
          new TestThread(i).start(); 
          Thread.sleep(1); 
      } 
    }  
}
Copy after login

Run results:

NO.0:887 
NO.0:888 
NO.0:889 
NO.0:890 
NO.0:891 
NO.0:892 
NO.0:893 
NO.0:894 
NO.7:122 
NO.7:123 
NO.7:124
Copy after login

The above is just a fragment to illustrate a problem.
Careful children's shoes will find that NO.0:894 is followed by NO.7:122, which means that it does not start from 0 to 999.
It is said that synchronized can implement synchronized methods or synchronized blocks. Why can't it work here?

Let’s first analyze the synchronization mechanism. Synchronization is achieved through locks. So in the above example, what objects or types are locked? There are two variables in it, one is i and the other is threadnum; i is internal to the method, and threadnum is private.
Let’s take a look at the synchronized operating mechanism:
In a Java program, when using a synchronized block or synchronized method, this area is marked for monitoring; while when the JVM is processing the program, when a program enters the monitoring area, it The object or class is automatically locked.

So in the above example, after the synchronized keyword is used, what is locked?
When the synchronized method is used, the instance object itself that calls the method is locked as the object lock. In this example, 10 threads have their own TestThread class objects created by themselves, so the object lock acquired is also their own object lock and has nothing to do with other threads.

To implement method locking, a shared object must be locked.

Modify the above example and take another look:

package thread_test; 
  
/** 
 * 测试扩展Thread类实现的多线程程序 
 * 
 */
public class TestThread extends Thread{  
  private int threadnum; 
  private String flag;  //标记 
    
  public TestThread(int threadnum,String flag) {  
       this.threadnum = threadnum;  
        this.flag = flag; 
    } 
    
  @Override
    public void run() {  
    synchronized(flag){ 
      for(int i = 0;i<1000;i++){  
              System.out.println("NO." + threadnum + ":" + i ); 
          }  
    } 
    }  
  
    public static void main(String[] args) throws Exception {  
      String flag = new String("flag"); 
      for(int i=0; i<10; i++){ 
          new TestThread(i,flag).start(); 
          Thread.sleep(1); 
      } 
    }  
}
Copy after login

A shared flag is also added. Then synchronize the flag flag through the synchronized block; this meets the conditions for locking the shared object.
Yes, the running results have come in order.

Through the synchronized block, specify the object lock to achieve the purpose of synchronization. Is there any other way to achieve this through the synchronized method?

According to the principle of synchronization: If you can obtain a shared object lock or class lock, synchronization can be achieved. So can we achieve it by sharing a class lock?

Yes, we can use static synchronization methods. According to the characteristics of static methods, it can only be called by the class object itself, and cannot be called by instantiating a class object. Then if the lock of this static method is obtained, this class lock is obtained, and these class locks are all TestThread class locks, and the purpose of obtaining the shared class lock is achieved.

The implementation code is as follows:

package thread_test; 
  
/** 
 * 测试扩展Thread类实现的多线程程序 
 * 
 * @author ciding 
 * @createTime Dec 7, 2011 9:37:25 AM 
 * 
 */
public class TestThread extends Thread{  
  private int threadnum; 
    
  public TestThread(int threadnum) {  
    this.threadnum = threadnum;  
  } 
    
  public static synchronized void staticTest(int threadnum) {  
    for(int i = 0;i<1000;i++){  
      System.out.println("NO." + threadnum + ":" + i ); 
    }  
  }  
  
  public static void main(String[] args) throws Exception {  
    for(int i=0; i<10; i++){ 
      new TestThread(i).start(); 
      Thread.sleep(1); 
    } 
  }  
    
  @Override
  public void run(){ 
    staticTest(threadnum); 
  } 
}
Copy after login

The running results are the same as in the second example.


The above content mainly explains two issues: synchronization block and synchronization method.
1, Synchronization block: The object lock acquired is the flag object lock in synchronized(flag).
2, Synchronous method: What is obtained is the class object to which the method belongs and the class object lock.
Static synchronization method, since multiple threads will share it, it will definitely be synchronized.
Instead of a static synchronization method, it will only be synchronized in singleton mode.


For more explanations on the basic usage of the synchronized keyword in Java multi-threaded programming, please pay attention to the PHP Chinese website!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles