Java内存模型:编译器重新排列代码行 [英] Java memory model : compiler rearranging code lines

查看:117
本文介绍了Java内存模型:编译器重新排列代码行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,只要重新排序对代码语义没有区别,Java语言允许编译器重新排列已编译代码行。但是,编译器只需要从当前线程中看到的麻烦。如果此重新排序影响多线程情况下的语义,则通常会导致并发问题(内存可见性)

It is well known that Java Language allows compliers to re-arrange lines of compiled code as long as the re-order makes no difference to the code semantics. However , the compiler is required to only bother about sematics as seen from the current thread. If this re-order affects semantics in a multithreaded situation , it usually causes concurrency issues ( memory visibility )

我的问题:


  1. 通过将此freedm允许到编译器可以实现什么?编译器是否真的可以通过重新排列代码来生成更高效的代码?我还没有看到一个实际案例。我觉得有时会带来的好处远远超过它可能引入的并发风险。

  1. What is achieved by allowing this freedm to the compiler ? Is it really possible for the compiler to produce code which is more efficient by rearranging the code ? I am yet to see a practical case for this. I feel sometimes that the benefits if any are far outweighed by the concurrency risks this can introduce.

程序员有没有办法告诉编译器不要重新排列行像这样 ?我知道使用同步原语有效地处理重新排列的副作用,但我问是否有任何直接的方法(编译器选项)来关闭它?

Is there any way that programmer can tell compiler not to rearrange lines like this ? I know that using synchronization primitives effectively handles the side-effects of rearranging , but I am asking if there is any direct way ( compiler option ) to turn this off ?


推荐答案

javac 编译器在没有优化的情况下执行。

The javac compiler has performs next to no optimisations.

JIT本机编译器可以在存在内存排序问题的情况下重新排序指令。但是,CPU也可以重新排序具有相同效果的指令和内存更新。

The JIT native compiler could re-order instructions where there is a memory ordering problem. However, the CPU also can re-order instructions and memory updates which have the same effect.


通过允许此freedm到达编译器?

What is achieved by allowing this freedm to the compiler ?

主要的好处是代码可移植性。您提供的保证越多,确保每个平台实际执行此操作的难度就越大。

The main benefit is code portability. The more guarantees you provide, the more difficult it is to ensure every platform actually does this.

通过允许CPU执行指令,可以显着提高性能。当它可以而不是严格的顺序。

There is also a significant performance improvement by allowing the CPU to execute instructions as and when it can rather than in a strict order.


编译器是否真的可以通过重新排列代码来生成更高效的代码?

Is it really possible for the compiler to produce code which is more efficient by rearranging the code ?

是的。但是由CPU完成的重新排序更为重要。

Yes. but the re-ordering done by the CPU is more significant.


我还没有看到一个实际案例。我觉得有时可以带来的并发风险远远超过它带来的好处。

I am yet to see a practical case for this. I feel sometimes that the benefits if any are far outweighed by the concurrency risks this can introduce.

程序员有没有办法告诉编译器不要重新排列这样的行?

Is there any way that programmer can tell compiler not to rearrange lines like this ?

这就是你使用内存障碍的原因,比如 volatile 同步块和锁定。当你使用这些时,你得到线程安全保证。

This is why you use memory barriers like volatile, synchronized blocks and Lock. When you use these you get thread safety guarantees.


我知道使用同步原语有效地处理重新排列的副作用,但我是询问是否有任何直接的方法(编译器选项)将其关闭?

I know that using synchronization primitives effectively handles the side-effects of rearranging , but I am asking if there is any direct way ( compiler option ) to turn this off ?

你可以关闭JIT,但大多数都是订购由CPU完成,因此不会实现太多。

You can turn off the JIT, but most re-ordering is done by the CPU so it wouldn't achieve much.

避免重新订购更新是线程安全问题的一小部分(其最大的问题是这是模糊的,很少发生,这使得测试很难)一旦你编写线程安全的代码,这是缓解的。

Avoiding re-ordering of updates is such a small part of the thread safety problem (its biggest issue is that is obscure and rarely occurs which makes testing it hard) And once you write thread safe code, this is alleviated.

这篇关于Java内存模型:编译器重新排列代码行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆