In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
本篇内容主要讲解"如何实现指令重排序",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"如何实现指令重排序"吧!
(一)什么是指令重排序
为了使处理器内部的运算单元能尽量被充分利用,处理器可能会对输入的代码进行乱序执行优化,处理器会在计算之后将乱序执行的结果重组,并确保这一结果和顺序执行结果是一致的,但是这个过程并不保证各个语句计算的先后顺序和输入代码中的顺序一致。这就是指令重排序。
简单来说,就是指你在程序中写的代码,在执行时并不一定按照写的顺序。
在Java中,JVM能够根据处理器特性(CPU多级缓存系统、多核处理器等)适当对机器指令进行重排序,最大限度发挥机器性能。
Java中的指令重排序有两次,第一次发生在将字节码编译成机器码的阶段,第二次发生在CPU执行的时候,也会适当对指令进行重排。
(二)复现指令重排序
光靠说不容易看出现象,下面来看一段代码,这段代码网上出现好多次了,但确实很能复现出指令重排序。我把解释放在代码后面。
public class VolatileReOrderSample { //定义四个静态变量 private static int x=0,y=0; private static int a=0,b=0; public static void main(String[] args) throws InterruptedException { int i=0; while (true){ i++; x=0;y=0;a=0;b=0; //开两个线程,第一个线程执行a=1;x=b;第二个线程执行b=1;y=a Thread thread1=new Thread(new Runnable() { @Override public void run() { //线程1会比线程2先执行,因此用nanoTime让线程1等待线程2 0.01毫秒 shortWait(10000); a=1; x=b; } }); Thread thread2=new Thread(new Runnable() { @Override public void run() { b=1; y=a; } }); thread1.start(); thread2.start(); thread1.join(); thread2.join(); //等两个线程都执行完毕后拼接结果 String result="第"+i+"次执行x="+x+"y="+y; //如果x=0且y=0,则跳出循环 if (x==0&&y==0){ System.out.println(result); break; }else{ System.out.println(result); } } } //等待interval纳秒 private static void shortWait(long interval) { long start=System.nanoTime(); long end; do { end=System.nanoTime(); }while (start+interval>=end); }}
这段代码虽然看着长,其实很简单,定义四个静态变量x,y,a,b,每次循环时让他们都等于0,接着用两个线程,第一个线程执行a=1;x=b;第二个线程执行b=1;y=a。
这段程序有几个结果呢?从逻辑上来讲,应该有3个结果:
当第一个线程执行到a=1的时候,第二个线程执行到了b=1,最后x=1,y=1;
当第一个线程执行完,第二个线程才刚开始,最后x=0,y=1;
当第二个线程执行完,第一个线程才开始,最后x=1,y=0;
理论上无论怎么样都不可能x=0,y=0;
但是当程序执行到几万次之后,竟然出现了00的结果:
This is because the instructions are reordered, x=b being executed before a=1 and y=a being executed before b=1.
(iii) By what means is instruction reordering prohibited?
Volatile prohibits instruction reordering by means of a memory barrier, which is a CPU instruction that guarantees the order in which certain operations are executed.
There are four types of memory barriers:
StoreStore barrier, StoreLoad barrier, LoadLoad barrier, LoadStore barrier.
JMM has rules for Volatile reordering for compilers:
It may not be easy to understand these theories, but I will explain them in common terms:
The first is the understanding of four kinds of memory barriers, Store is equivalent to write barrier, Load is equivalent to read barrier.
For example, there are two lines of code, a=1;x=2; and I modify x to volatile.
When a=1 is executed, it is equivalent to performing an ordinary write operation;
When x=2 is executed, it is equivalent to performing a volatile write operation;
So between these two lines, a StoreStore barrier (write before and write after) is inserted, which is the memory barrier.
If the first operation is normal write and the second operation is volatile write, then the corresponding value in the table is NO, and reordering is prohibited. This is how Volatile reorders instructions.
Now, we just need to modify x and y in the above code with volatile, and no instruction reordering will occur (if you can push the logic through the table, you will understand).
At this point, I believe everyone has a deeper understanding of "how to achieve instruction reordering," so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.