Home >Java >javaTutorial >How to use java synchronization method

How to use java synchronization method

王林
王林forward
2023-04-20 10:58:141085browse

1. For ordinary synchronization methods, the lock will be associated with the object that calls the method.

2. For static synchronization methods, the lock is a monitor related to the declaring method class object.

3. The easiest way to create a synchronized block is to declare the method as synchronized.

This means that the caller must acquire the lock before entering the method body.

Example

public class Point {
  public synchronized void setXY(int x, int y) {
    this.x = x;
    this.y = y;
  }
}

The above is the detailed content of How to use java synchronization method. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete