What you need to know to get started with Java Virtual Machine

  As the saying goes, workers must first sharpen their tools if they want to do the right thing. For Java developers, a simple understanding of certain features of the Java Virtual Machine is beneficial to improve the internal power of developers. Why do we need to install and configure the JDK before writing Java code? Why are these packages built into JDK so powerful? Why is there a memory leak in our program? Why doesn't the problem of data inconsistency appear when multi-threaded access program is added with Synchronized keyword in the code? Many similar questions will be answered after we understand the JVM.

                                             

 "In-depth understanding of Java Virtual Machine" by Zhou Zhiming This book is generally recommended by everyone.

main component:

                                         

public class happenBeforeDemo {
    private int value = 0;
    public int getValue() {
        return value;
    }
    public void setValue(int value) {
        this.value = value;
    }
    public static void main(String[] args) {
        happenBeforeDemo happenBeforeDemo = new happenBeforeDemo();
        new Thread(() -> happenBeforeDemo.setValue(22)).start();
        new Thread(() -> System.out.println(happenBeforeDemo.getValue())).start();
    }
}

 

Everyone guess what the output is? 0 or 1? And what principle does this code follow?

What is the principle of advance concurrency?

    Program sequence rules Manage lock rules volatile variable rules Thread start rules Thread termination rules Thread interrupt rules Object termination rules Transitivity 

    The problem of measuring thread safety is not to be disturbed by time sequence, but everything is based on the principle of pre-occurrence

    Because the first thread and the second thread call the setValue () and getValue () functions respectively, they are not in the same thread, and the program order rules do not apply. Since there is no synchronization block, lock operations and unlock operations will naturally not occur. Tube lock is not suitable. Since the value is not modified with volatile, there is naturally no volatile variable rule. The following thread startup, thread termination, thread interruption, and object termination rules are also irrelevant here. The last transitivity does not apply. Therefore, this operation is a thread-unsafe operation.

Solutions are more common

/ ** 
 * Thread A accesses getValue Thread B accesses setValue 
 * Solution synchronized volatile 
 * /

 

We know that this code will eventually be compiled into a .class file. Before we look at the bytecode, first understand the following simple knowledge.

Bytecode format description:

1: Version of Magic and Class files 2: Constant pool 3: Access flag 4: Class index parent index and interface index collection 5: Field table collection 6: Method table collection 7: Property table collection (1code 2Exceptions 3 LineNumberTable 4 LocalVariableTable 5 SourceFile property 6 ConstantValue attribute 7 InnerClasses 8 Deprecated and Synthetic attributes 9 StackMapTable attribute (added after JDK1.6) 10 Signature attribute (added after JDK1.5) 11 BootstrapMethods attribute (added after JDK1.7)). . . .

https://mp.weixin.qq.com/s?__biz=MzUzNTY1MzE3NA==&mid=2247484019&idx=1&sn=eeee2f3bca2ce7bcc7c156bc24dc553c&chksm=fa837c2acdf4f53cdf4a3c929914671837b495154a0dfb76cbc3da435faae112624c050acc32&token=1666815065&lang=zh_CN#rd

More attention to WeChat public account  

                                                            

Published 24 original articles · praised 36 · 20,000+ views

Guess you like

Origin blog.csdn.net/tanjunchen/article/details/99710694