Java geek

Related reading:

Java Concurrent Programming (1) Knowledge Map Java concurrent programming (2) Atomic Java Concurrent programming (3) Visibility Java Concurrent programming (4) Sequential Java Concurrent programming (5) Overview of how to create Threads Java Concurrent programming (7) Easy to understand wait and Notify and use scenarios Introduction to Concurrent Programming in Java (8) Thread Lifecycle Introduction to Concurrent Programming in Java (9) Deadlock and deadlock bits Introduction to Concurrent programming in Java (10) Lock Optimization Introduction to Concurrent programming in Java (11) Flow limiting Scenarios and Spring Flow Limiter Implementation introduction to Concurrent programming in Java (12) Producer and Consumer Patterns – Code templates Java Concurrent programming introduction (13) Read/write lock and cache template Java concurrent programming Introduction (14) CountDownLatch Application Scenario Java concurrent programming Introduction (15) CyclicBarrier application scenario Java concurrent programming Introduction (16) Understand thread pool differences Introduction to Concurrent Programming in Java (17) 一 figure Master common classes and interfaces for threads Concurrent programming in Java (18) Rediscussion of thread safety Concurrent programming in Java (19) Asynchronous task scheduling tool CompleteFeature Concurrent programming in Java (20) Common locking scenarios and locking tools


First, scope

The scopes of synchronized are as follows: 1. Static methods

    public static synchronized void increase(a) {
        count = count + 1;
    }
Copy the code

2. Instance method

    public synchronized int getCount(a) {
        return count;
    }
Copy the code

3. The code block

    public static void decrease(a) {
        synchronized (SynchronizedDemo.class) {
            count = count - 1; }}Copy the code

Two, add lock object

Synchronized Objects are as follows: 1. The locked object is class 2. The locked objects are class instance objects.

public class SynchronizedDemo {

    private static int count = 0;

    synchronizedDemo.class
    public static synchronized void increase(a) {
        count = count + 1;
    }

    public static void decrease(a) {
        synchronizedDemo.class
        synchronized (SynchronizedDemo.class) {
            count = count - 1; }}The lock object is an instance of the SynchronizedDemo class
    public synchronized int getCount(a) {
        return count;
    }

    public void print(a) {
        // Lock an instance of the SynchronizedDemo class
        synchronized (this) { System.out.println(count); }}}Copy the code

Iii. Key Points

1. Synchronized is mutually exclusive and blocked. If USER A has obtained the lock, user B can execute the lock only after user A finishes. 2. If the locked objects are not the same, they will not be blocked. For example, static methods and instance methods of the same class have the synchronized keyword, but they are not the same lock and do not affect each other. 3. The locked object must be unchanged; otherwise, the lock may change, resulting in incorrect processing results during concurrent processing.

end.


<– Read the mark, left like!