简体   繁体   English

Java语句的执行顺序

[英]Order of execution java statements

Since order of execution is not guaranteed. 由于不能保证执行顺序。 In below program order of execution for main method can be like below ? 在下面的程序中,主要方法的执行顺序可以如下?

 t2.start();
 t2.join(); 
 t1.start();
 t1.join(); 

Program is: 程序是:

public class Puzzle {
  static boolean answerReady = false;
  static int answer = 0;
  static Thread t1 =
      new Thread() {
        public void run() {
          answer = 42;
          answerReady = true;
        }
      };
  static Thread t2 =
      new Thread() {
        public void run() {
          while (!answerReady) Thread.sleep(100);
          System.out.println("The meaning of life is: " + answer);
        }
      };

  public static void main(String[] args) throws InterruptedException {
    t1.start();
    t2.start();
    t1.join();
    t2.join();
  }
}

Edit: want to add few things after seeing comments 编辑:想看到评论后添加一些东西

  1. answerReady may never become true. 就绪可能永远不会成为现实。 Agree. 同意。
  2. what are special conditions when order of execution can be changed ? 可以更改执行顺序的特殊条件是什么?
  3. why main method is correctly synchronized here ? 为什么main方法在这里正确同步?

The Java Language Specification dictates what conforming JVMs may do or not. Java语言规范规定了符合条件的JVM可以做什么或不能做什么。 See 看到

§17.4.5. §17.4.5。 Happens-before Order 发生在订单之前

Two actions can be ordered by a happens-before relationship. 可以通过事前发生关系来排序两个动作。 If one action happens-before another, then the first is visible to and ordered before the second. 如果一个动作发生在另一个动作之前 ,则第一个动作对第二个动作可见,并在第二个动作之前排序。

If we have two actions x and y , we write hb(x, y) to indicate that x happens-before y . 如果我们有两个动作xy ,我们写hb(x,y)表示x发生在y之前

  • If x and y are actions of the same thread and x comes before y in program order, then hb(x, y) . 如果xy是同一线程的动作,并且x按程序顺序位于y之前,则hb(x,y)
  • ...

Since your invocations of start() and join() are actions of the same thread, they are ordered in respect to the program order. 由于您对start()join()的调用是同一线程的操作,因此它们相对于程序顺序是有序的。

I think, it should be obvious that if that simple guaranty didn't exist, even single threaded programming was impossible. 我认为,很明显,如果没有这种简单的保证,就连单线程编程也是不可能的。

This does not imply the absence of reordering in the code. 这并不意味着代码中没有重新排序。 It only implies that such optimizations must happen in a way that retains the observable behavior of these actions when executing this code. 它仅意味着此类优化必须以在执行此代码时保留这些动作可观察到的行为的方式进行。

The point here is, that while the main thread will consistently do what you told it to do, other threads not having a happens-before relationship to either of these actions, might not see the actions in the same way. 这里的要点是,尽管主线程将始终如一地执行您要求的操作,但其他线程与这两个操作中的任何一个都不具有先发生后关系,可能不会以相同的方式看到这些操作。 In your case, with the three threads shown, there are several relationships: 就您而言,在显示了三个线程的情况下,存在几种关系:

continuation of §17.4.5 §17.4.5的延续

  • ...
  • If hb(x, y) and hb(y, z) , then hb(x, z) . 如果hb(x,y)hb(y,z) ,则hb(x,z)

...

  • A call to start() on a thread happens-before any actions in the started thread. 在启动线程中的任何操作之前,都会在线程上调用start()
  • All actions in a thread happen-before any other thread successfully returns from a join() on that thread. 线程中的所有操作都会发生-在任何其他线程成功从该线程上的join()返回之前

From this you can derive that all three threads of your code agree on what the main thread is doing in most parts. 从中可以得出,代码的所有三个线程在大多数部分都同意主线程的工作。

Of course, this doesn't change the fact that the two spawned threads are improperly (not at all) synchronized and t2 may print the value 0 instead of 42 or never terminate at all. 当然,这不会改变以下事实:两个产生的线程不正确地同步(根本不同步),并且t2可能会打印值0而不是42或者根本不会终止。

No. the order of execution on the main thread is as you've declared it in main : 不。主线程的执行顺序与您在main声明的顺序相同:

t1.start();
t2.start();
t1.join();
t2.join();

The only thing that's not guaraneed is the content of the threads t1 and t2 . 唯一没有保证的是线程t1t2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM