search
HomeJavajavaTutorialDetailed introduction to the use of Java synchronized block

Java synchronized block (synchronized block) is used to mark methods or code blocks as synchronized. Java synchronized blocks are used to avoid races. This article introduces the following content:

  • Java synchronization keyword (synchronzied)

  • Instance method synchronization

  • Static method synchronization

  • Synchronization block in instance method

  • Synchronization block in static method

  • Java synchronization example

Java synchronization keyword (synchronized)

Synchronized blocks in Java are marked with synchronized. A synchronized block in Java is synchronized on a object. All synchronized blocks synchronized on an object can only be entered and performed by one thread at the same time. All other threads waiting to enter this synchronized block will be blocked until the thread executing this synchronized block exits.

There are four different synchronization blocks:

  1. Instance methods

  2. Static methods

  3. Synchronized blocks in instance methods

  4. Synchronized blocks in static methods

The above synchronized blocks are all synchronized on different objects. Which synchronization block is actually needed depends on the situation.

Instance method synchronization

The following is a synchronized instance method:

public synchronized void add(int value){
this.count += value;
 }

Note the synchronized keyword in the method declaration. This tells Java that the method is synchronous.

Java instance method synchronization is synchronized on the object that owns the method. In this way, the method synchronization of each instance is synchronized on a different object, that is, the instance to which the method belongs. Only one thread can run within an instance method synchronized block. If multiple instances exist, a thread can perform operations in one instance synchronized block at a time. One thread per instance.

Static method synchronization

Static method synchronization is the same as the instance method synchronization method, and also uses the synchronized keyword. Java static method synchronization is as follows:

public static synchronized void add(int value){
 count += value;
 }

Similarly, the synchronized keyword here tells Java that this method is synchronized.

The synchronization of static methods refers to synchronization on the class object where the method is located. Because a class can only correspond to one class object in the Java virtual machine, only one thread is allowed to execute static synchronization methods in the same class at the same time.

For static synchronized methods in different classes, one thread can execute the static synchronized methods in each class without waiting. Regardless of which static synchronization method in the class is called, a class can only be executed by one thread at the same time.

Synchronized blocks in instance methods

Sometimes you don’t need to synchronize the entire method, but a part of the method. Java can synchronize part of a method.

An example of a synchronized block in an asynchronous Java method is as follows:

public void add(int value){

    synchronized(this){
       this.count += value;
    }
  }

The example uses the Java synchronized block constructor to mark a piece of code as synchronized. This code executes like a synchronous method.

Note that the Java synchronized block constructor encloses the object in parentheses. In the above example, "this" is used, which is the instance itself that calls the add method. The object enclosed in parentheses in the synchronized constructor is called a monitor object. The above code uses monitor object synchronization, and the synchronized instance method uses the instance of the calling method itself as the monitor object.

Only one thread can execute within a Java method synchronized to the same monitor object at a time.

The following two examples synchronize the instance objects they are called, so they are equivalent in synchronization execution effects.

public class MyClass {

   public synchronized void log1(String msg1, String msg2){
      log.writeln(msg1);
      log.writeln(msg2);
   }

   public void log2(String msg1, String msg2){
      synchronized(this){
         log.writeln(msg1);
         log.writeln(msg2);
      }
   }
 }

In the above example, only one thread can execute in either method of the two synchronized blocks at a time.

If the second synchronization block is not synchronized on this instance object, then the two methods can be executed by the thread at the same time.

Synchronization blocks in static methods

Similar to the above, the following is an example of synchronization of two static methods. These methods are synchronized on the class object to which the method belongs.

public class MyClass {
    public static synchronized void log1(String msg1, String msg2){
       log.writeln(msg1);
       log.writeln(msg2);
    }

    public static void log2(String msg1, String msg2){
       synchronized(MyClass.class){
          log.writeln(msg1);
          log.writeln(msg2);
       }
    }
  }

These two methods are not allowed to be accessed by threads at the same time.

If the second synchronization block is not synchronized on the MyClass.class object. Then these two methods can be accessed by threads at the same time.

Java Synchronization Example

In the following example, two threads are started, both calling the add method of the same instance of the Counter class. Because the synchronization is on the instance to which the method belongs, only one thread can access this method at the same time.

public class Counter{
     long count = 0;

     public synchronized void add(long value){
       this.count += value;
     }
  }
  public class CounterThread extends Thread{

     protected Counter counter = null;

     public CounterThread(Counter counter){
        this.counter = counter;
     }

     public void run() {
    for(int i=0; i<10; i++){
           counter.add(i);
        }
     }
  }
  public class Example {

    public static void main(String[] args){
      Counter counter = new Counter();
      Thread  threadA = new CounterThread(counter);
      Thread  threadB = new CounterThread(counter);

      threadA.start();
      threadB.start();
    }
  }

创建了两个线程。他们的构造器引用同一个Counter实例。Counter.add方法是同步在实例上,是因为add方法是实例方法并且被标记上synchronized关键字。因此每次只允许一个线程调用该方法。另外一个线程必须要等到第一个线程退出add()方法时,才能继续执行方法。

如果两个线程引用了两个不同的Counter实例,那么他们可以同时调用add()方法。这些方法调用了不同的对象,因此这些方法也就同步在不同的对象上。这些方法调用将不会被阻塞。如下面这个例子所示:

public class Example {

   public static void main(String[] args){
     Counter counterA = new Counter();
     Counter counterB = new Counter();
     Thread  threadA = new CounterThread(counterA);
     Thread  threadB = new CounterThread(counterB);

     threadA.start();
     threadB.start();
   }
 }

注意这两个线程,threadA和threadB,不再引用同一个counter实例。CounterA和counterB的add方法同步在他们所属的对象上。调用counterA的add方法将不会阻塞调用counterB的add方法。

The above is the detailed content of Detailed introduction to the use of Java synchronized block. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
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 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 to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

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 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...

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...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools