java - Java 代码在单个线程中的执行是否保证是顺序的?

标签 java android c++ c

在 Java、C 和 C++ 中,源代码是否保证在单个给定线程内逐行顺序执行,即使在编译器优化之后也是如此?如果允许系统重新排序您的代码,似乎什么都不会起作用,但我似乎找不到任何文档来保证如果我在 Java 中有以下内容:

class MyClass{
String testString = "";

public MyClass(){

}

public void foo(){
    testString = "foo";
}
public void bar(){
    testString = "bar";
    testString += "r";
}
public String getTestString(){
    return testString;
}

}

class Main{
static void main(String[] args){
    MyClass testClass = new MyClass();
    testClass.foo();
    System.out.println(class.getTestString());
    testClass.bar();
    System.out.println(class.getTestString());
}
}

输出永远是

"foo"
"barr"

永远不会

"foo"
"rbar"

或如果其中的方法调用和语句未按照源代码中指定的顺序执行,则可能出现的任何其他可能的变化。

这个问题特别针对 Java 出现,因为它大大减少了程序员对目标系统上的字节码编译器和 JIT 编译器或解释器将对您的代码执行的操作的控制。对我来说主要的系统是 Android,我为此实现了自己的信号量和互斥锁定机制(例如,没有过多使用内置的 Java 并发机制,如“synchronized”和“volatile”关键字),这更适合我的应用程序比 Java 提供的应用程序。然而,一位 friend 警告我,由于 Java 从源代码到机器代码经历了多个级别的转换,除非我使用 Java 的内置并发机制,否则无法保证我的信号量和锁定实现会按我的预期执行。这实际上归结为是否有特定的保证,对于任何给定的运行时实现,代码的执行将在单个线程中按顺序执行。所以主要的问题是:

  1. 在 C 和 C++ 中,尽管进行了编译器优化,代码执行是否保证顺序执行?如果不是,禁用编译器优化是否足以实现这样的保证?

  2. 在 Java 中,尽管字节码编译器和 JIT 编译器或解释器可能会进行更改(特别是在 Android 上运行,但也适用于任意 VM 实现),代码执行是否保证顺序执行?

  3. 如果如我所料,以上问题的答案是肯定的,是否有任何编程语言/平台/上下文不能保证在单个线程内顺序执行?

最佳答案

如果只有一个线程,您的代码将具有直观预期的结果。任何优化都必须保留优化前的功能。只有当您有多个线程时,才会发生意想不到的事情。

Java 语言规范主要处理存在多个线程时出现的违反直觉的行为,您的问题是关于此处定义的“线程内语义”:

The memory model determines what values can be read at every point in the program. The actions of each thread in isolation must behave as governed by the semantics of that thread, with the exception that the values seen by each read are determined by the memory model. When we refer to this, we say that the program obeys intra-thread semantics. Intra-thread semantics are the semantics for single-threaded programs, and allow the complete prediction of the behavior of a thread based on the values seen by read actions within the thread. To determine if the actions of thread t in an execution are legal, we simply evaluate the implementation of thread t as it would be performed in a single-threaded context, as defined in the rest of this specification.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html

关于java - Java 代码在单个线程中的执行是否保证是顺序的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15027925/

相关文章:

java - 在JAVA字体颜色中使用HTML Color代码

c++ - 分配 native C++ 结构的 CX 公共(public)值结构内容

c++ - 使用相同流对象的 io 操作

Android WorkManager 同时启动太多作业

c++ - 在 Linux 上使用 QTCreator IDE 的文件 IO

java - TALEND 根据 TalendDate 设置上下文变量

java - 使用 Comparable 接口(interface)比较两个日期

java - 监听短信,在j2me应用程序中发送的短信没有任何端口号

android - 如何在 Parse.com 中使用本地数据存储

android - 通知彩色操作按钮 Android 10