Java线程运行序列

Java线程运行序列,java,multithreading,Java,Multithreading,我是JAVA新手,在学习JAVA complete reference时,我遇到了以下代码 class DemoThread implements Runnable { String name; // name of thread Thread t; DemoThread(String threadname) { name = threadname; t = new Thread(this, name); System.out.println("New th

我是JAVA新手,在学习JAVA complete reference时,我遇到了以下代码

class DemoThread implements Runnable {
  String name; // name of thread
  Thread t;

  DemoThread(String threadname) {
    name = threadname;
    t = new Thread(this, name);
    System.out.println("New thread: " + t);
    t.start(); // Start the thread
  }

  // This is the entry point for thread.
  public void run() {
    try {
      for(int i = 5; i > 0; i--) {
        System.out.println(name + ": " + i);
        Thread.sleep(1000);
      }
    } catch (InterruptedException e) {
      System.out.println(name + " interrupted.");
    }
    System.out.println(name + " exiting.");
  }
}

class DemoJoin {
  public static void main(String args[]) {
    DemoThread ob1 = new DemoThread("One");
    DemoThread ob2 = new DemoThread("Two");
    DemoThread ob3 = new DemoThread("Three");

    System.out.println("Thread One is alive: "
                        + ob1.t.isAlive());
    System.out.println("Thread Two is alive: "
                        + ob2.t.isAlive());
    System.out.println("Thread Three is alive: "
                        + ob3.t.isAlive());
    // wait for threads to finish
    try {
      System.out.println("Waiting for threads to finish.");
      ob1.t.join();
      ob2.t.join();
      ob3.t.join();
    } catch (InterruptedException e) {
      System.out.println("Main thread Interrupted");
    }

    System.out.println("Thread One is alive: "
                        + ob1.t.isAlive());
    System.out.println("Thread Two is alive: "
                        + ob2.t.isAlive());
    System.out.println("Thread Three is alive: "
                        + ob3.t.isAlive());

    System.out.println("Main thread exiting.");
  }
}
书中给出的假定输出是

New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
One: 5
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Two: 3
Three: 3
One: 2
Two: 2
Three: 2
One: 1
Two: 1
Three: 1
Two exiting.
Three exiting.
One exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.
但当我在Netbeans中运行此代码时,我得到了

run:
New thread: Thread[One,5,main]
New thread: Thread[Two,5,main]
New thread: Thread[Three,5,main]
One: 5
Thread One is alive: true
Thread Two is alive: true
Thread Three is alive: true
Waiting for threads to finish.
Two: 5
Three: 5
One: 4
Two: 4
Three: 4
One: 3
Three: 3
Two: 3
One: 2
Three: 2
Two: 2
One: 1
Three: 1
Two: 1
One exiting.
Three exiting.
Two exiting.
Thread One is alive: false
Thread Two is alive: false
Thread Three is alive: false
Main thread exiting.
BUILD SUCCESSFUL (total time: 5 seconds)

为什么
One:5
介于
新线程:线程[Three,5,main]
线程一是活动的:true

关于这个示例,只有一件事可以说,即只有在线程1,2和3完成后,主线程才会退出。您不能保证线程1、线程2和线程3的执行顺序。事实上,您多次运行此代码,您可能会在不同的执行中看到不同的顺序

新线程:线程[Three,5,main]->表示主线程产生了第三个线程。此时,所有三个线程都已生成,它们可能已经运行了

One:5->线程1正在执行

线程一处于活动状态:true->这是在主线程中执行的。在看到以下几行之后,您可能已经看到了这一点:

2:5


三:5

这个例子的背景是什么?除非我遗漏了一些代码不能保证线程执行顺序的东西(这意味着多次运行可能会导致不同的输出)。但我的问题是为什么没有遵循序列,并且在实例化之后第一个线程开始运行。该示例无效。此代码中没有任何东西可以保证输出。
demohread
的构造函数启动其成员
Thread
。“is-alive”语句发生在构造函数调用之后。首先调用哪些打印语句取决于线程调度,因为给定的代码不能保证顺序。您提到的示例应该显示
isAlive
join
use;它做到了。但是这个例子是否真的保证了每次输出都是相同的呢?因为如果是这样的话,那么user207421是正确的,而示例是无效的(或者至少部分错误)。是的,它每次都给出不同的结果集。但是当我研究这本书的时候,它并没有明确提到只有这个顺序会遵循。我想当我们调用
demohread ob1=newdemohread(“One”)对象已创建,但run方法未启动。我想
ob1.t.join()开始运行线程。所以我感到困惑。