synchronized from entry to the source code analysis

Micro-channel public number: the Java patients

Synchronized

synchronized Profile

synchronized keyword is a java, java is the language in order to solve atomicity, visibility and order problems in presence of concurrent programming, provides a range of related keywords with concurrent processing, we need to take a brief look synchronized today .

How lock?

package com.zero.day3;

/**
* @Description: synchronized
* @Author: Zero
* @Date: 2019/12/10
*/
public class SynchronizedDemo1 {




    // 同步方法
    public synchronized void wc1 () {
        System.out.println("我在上厕所1");
    }


    // 同步方法 (静态)
    public static void wc2 () {
        synchronized (SynchronizedDemo1.class) {
            System.out.println("我在上厕所2");
        }
    }


    // 同步代码块
    public void wc3 () {
        synchronized (this) {
            System.out.println("我在上厕所3");
        }
    }

}

复制代码

When the above three simple method has been added to the general synchronization method: lock the synchronized keyword is the current instance of the object, that is, at the same time, each method can only be accessed by a single thread, a toilet at the same time only one person in use. Static synchronization method: lock the current class is an object class (because static methods run before the object, when the operation may not have a static method the object, so the object of the current class is the class) Block synchronization method: the lock object is parentheses

synchronized implementation principle

We now decompile the operation of the above methods

LINENUMBER 8 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init> ()V
    RETURN
   L1
    LOCALVARIABLE this Lcom/zero/day3/SynchronizedDemo1; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 0x21
  public synchronized wc1()V
   L0
    LINENUMBER 15 L0
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "\u6211\u5728\u4e0a\u5395\u62401"
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
   L1
    LINENUMBER 16 L1
    RETURN
   L2
    LOCALVARIABLE this Lcom/zero/day3/SynchronizedDemo1; L0 L2 0
    MAXSTACK = 2
    MAXLOCALS = 1

  // access flags 0x9
  public static wc2()V
    TRYCATCHBLOCK L0 L1 L2 null
    TRYCATCHBLOCK L2 L3 L2 null
   L4
    LINENUMBER 21 L4
    LDC Lcom/zero/day3/SynchronizedDemo1;.class
    DUP
    ASTORE 0
    MONITORENTER
   L0
    LINENUMBER 22 L0
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "\u6211\u5728\u4e0a\u5395\u62402"
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
   L5
    LINENUMBER 23 L5
    ALOAD 0
    MONITOREXIT
   L1
    GOTO L6
   L2
   FRAME FULL [java/lang/Object] [java/lang/Throwable]
    ASTORE 1
    ALOAD 0
    MONITOREXIT
   L3
    ALOAD 1
    ATHROW
   L6
    LINENUMBER 24 L6
   FRAME CHOP 1
    RETURN
    MAXSTACK = 2
    MAXLOCALS = 2

  // access flags 0x1
  public wc3()V
    TRYCATCHBLOCK L0 L1 L2 null
    TRYCATCHBLOCK L2 L3 L2 null
   L4
    LINENUMBER 29 L4
    ALOAD 0
    DUP
    ASTORE 1
    MONITORENTER
   L0
    LINENUMBER 30 L0
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    LDC "\u6211\u5728\u4e0a\u5395\u62403"
    INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
   L5
    LINENUMBER 31 L5
    ALOAD 1
    MONITOREXIT
   L1
    GOTO L6
   L2
   FRAME FULL [com/zero/day3/SynchronizedDemo1 java/lang/Object] [java/lang/Throwable]
    ASTORE 2
    ALOAD 1
    MONITOREXIT
   L3
    ALOAD 2
    ATHROW
   L6
    LINENUMBER 32 L6
   FRAME CHOP 1
    RETURN
   L7
    LOCALVARIABLE this Lcom/zero/day3/SynchronizedDemo1; L4 L7 0
    MAXSTACK = 2
    MAXLOCALS = 3
}

复制代码

We look carefully can actually find MONITORENTER with MONITOREXIT two words, then, I come to you with the ones who did this process relatively simple and crude language. If this time it has three threads Thread1, Thread2, Thread3 simultaneously to call a method, the code is certainly shared by you, then when this method function is not locked, Thread1 to execute this code, you can also perform a half Thread2 to perform, Thread3 of course, can also be performed. When this method plus the lock function is not the same after this code compiler will generate two instructions, when Thread1 to grab execution of the CPU to execute this method, ah, met this MONITORENTER time, this on the powerful ah! It gave another thread calls the wait method, when we all know that after the thread calls the wait method, will enter the wait state, no one can not save them this time on their own Thread1 blatantly executed after this locking method, naturally With MONITOREXIT command, this time thread is still waiting for? How to do, when the thread is in wait We all know notifyAll method will be called to wake up all the other threads, this time all the threads revived to continue.

Epilogue

Xiao Bian is a Java Coder, amateur writing articles, now the main micro-channel public number "Java patient," I like it concerns public micro-channel number or add me to study with us Java

Guess you like

Origin juejin.im/post/5df2666bf265da33f63f5015