Java线程运行顺序

时间:2018-12-20 01:11:11

标签: java multithreading

我是JAVA的新手,在学习JAVA完全参考时,我遇到了这段代码

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: 5New thread: Thread[Three,5,main]Thread One is alive: true之间出现的原因

1 个答案:

答案 0 :(得分:1)

关于此示例,只能说一件事-主线程仅在线程一,二和三完成后退出。您不能保证线程一,线程二和线程三的执行顺序。实际上,您多次运行此代码,可能会在不同的执行中看到不同的顺序。

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

一个:5 ->线程一正在执行

一个线程还活着:true->这是在Main线程中执行的事情。看到以下几行后,您可能已经看到了这一点:

二:5

三:5