Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of two stages of java instruction rearrangement phenomenon

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article shares with you the content of an example analysis of the two stages of java instruction rearrangement. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Compile-time instruction rearrangement

The interpretive language performs the compile + run action during the run, so the running efficiency is lower than that of the compiled language. Java can be used both as an interpretive language and as a compiled language. But the mainstream approach is to use it as a compiled language. Did Java do instruction rearrangement optimization at compile time? What optimizations have been made? Can I have a look? In order to satisfy everyone's curiosity, arrange.

Here we first explain the compilation time: for example, there is only one compilation time for cAccord Cobb +, that is, it calls the gcc command to compile the cCompact croup + code into assembly code.

But there are two compilation periods in Java:

1. Call the javac command to compile the Java code into Java bytecode

2. The gcc command is called on the Unix faction platform to compile the openjdk source code into assembly code.

All the articles on the Internet are about the first kind, and they are all about concepts. In this article, I not only talk about both, but also prove it to you in the way of code and pictures. So if you want to learn at the bottom, you can't learn without finding a reliable master, because first, you don't know how much this knowledge is involved, and the second two viewpoints are in front of you, and you don't know which is right or which is wrong.

Let me first give you the conclusion: during compilation, the so-called instruction rearrangement in Java mainly refers to the instruction rearrangement when compiling openjdk, and there is no instruction rearrangement when compiling Java code into Java bytecode. That is, whether you add volatile, the resulting bytecode file is the same. Does it subvert your understanding of this area? You don't believe me? Look at the case.

Someone may ask, if the bytecode files generated by adding volatile are all the same, how does JVM know at run time? When class attributes are stored in JVM, there is one property: Access flags. At run time, JVM uses this attribute to determine whether the class attribute of the operation has been modified by volatile, as shown above.

1. Use the mysterious code public class Test3 {public static / * volatile * / int found = 0; public static void main (String [] args) {new Thread (new Runnable () {public void run () {System.out.println ("and other friends send pens..."); while (0 = = found) {} System.out.println ("here comes the pen, start writing.") }, "I thread"). Start (); new Thread (new Runnable () {public void run () {try {Thread.sleep (2000);} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("my friend found the pen, send it."); change () }, "start thread"). Public static void change ();} public static void change () {found = 1;}}

Explain this code a little bit: there are two threads: my thread and my friend thread. The "I thread" blocks through a dead loop waiting for the "gay thread" to find a pen to deliver, and then starts writing. The "gay friend thread" waits for a while to find the pen and deliver it when it is found.

2. Compile to Java bytecode (without volatile)

3. Compile to Java bytecode (with volatile added)

You can find out whether or not to add volatile, the resulting bytecode is the same.

4. Compiler optimization

Instruction rearrangement is one of the compiler optimizations. Compiling openjdk enables O2-level compiler optimization, as shown in the figure.

What optimizations have been made for O2-level optimization? Such as optimizing invalid code, completing simple operations during compilation, and dealing with compile-time barriers. How many levels of optimization does gcc have? Interested children's shoes can learn by themselves, Baidu search keywords:-O2.

Optimize invalid code, look at the picture (I will not post C++ code)

Run-time instruction rearrangement

I don't know if you've ever heard of a word: CPU execution out of order. Out-of-order execution is relative to sequential execution. When the computer was first invented, it was executed sequentially, but later, in order to improve the efficiency of CPU operation, it was upgraded to out-of-order execution.

So why does out-of-order execution improve operational efficiency? If you are interested in children's shoes, you can study them. Key words: instruction pipeline.

So in the computer business, if you think the basic knowledge learned in college is not important, you can see how important it is if you read my article. At the end of the line, it is these things to see who studies more deeply, at the bottom and more clearly.

Because the current CPU is executed out of order, it brings the phenomenon of instruction rearrangement in the process of running the program. This is at run time, inside CPU, so I can't prove it to you. But even if out-of-order execution improves efficiency, it doesn't change the will of my program, which leads to a concept: as-if-serial.

What is as-if-serial? To put it simply, no matter how you rearrange instructions at compile time or at run time, the execution result of the program in a single-threaded environment cannot be changed. To put it bluntly, this is the bottom line of instruction rearrangement and a norm that must be followed. Then how to guarantee it? This leads to two other knowledge points that are difficult to understand: happens-before and memory barrier.

What does happens-before do? To put it simply, it is to tell the person who writes JVM that you should follow these rules when writing JVM, which are the rules that your JVM should do by default, rather than the programmer who needs to think about doing control when writing code. For example, the initialization of an object must be done before the execution of the finalize method. I will not elaborate on the other rules, all of which are easy to understand. Children's shoes will learn by themselves.

The order of some processes can be known and determined in advance, but the order of some processes cannot be known in advance, such as your company's business, JVM writers certainly do not know, so programmers still need to control according to business needs, then from the JVM level, I will provide you with the mechanism. The memory barrier is one of these mechanisms, and there are other locks.

Thank you for reading! This is the end of the article on "sample analysis of the two stages of java instruction rearrangement". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report