简体   繁体   English

Java线程运行顺序

[英]Java Thread run sequence

I am new to JAVA and while studying JAVA complete reference I came across this code 我是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.");
  }
}

The supposed output given in book is 本书给出的假设输出是

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.

But when I ran this code in Netbeans I got 但是当我在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)

How come One: 5 came in between New thread: Thread[Three,5,main] and Thread One is alive: true 怎么来One: 5介于New thread: Thread[Three,5,main]Thread One is alive: true

There is only one thing which can be said about this example is - the Main thread will exit only after completion of thread one, two and three. 关于此示例,只能说一件事-主线程仅在线程一,二和三完成后退出。 You can not guarantee the order of execution of Thread One, Thread Two, and Thread Three. 您不能保证线程一,线程二和线程三的执行顺序。 In fact, you run this code multiple times you may see different order in different execution. 实际上,您多次运行此代码,可能会在不同的执行中看到不同的顺序。

New thread: Thread[Three,5,main] -> Means main thread has spawned the third thread. 新线程:Thread [Three,5,main] ->表示主线程产生了第三个线程。 By this time all the three threads have been spawned and they may be running already. 到这时,所有三个线程都已生成,它们可能已经运行。

One: 5 -> Thread One is executing 一:5- >线程一正在执行

Thread One is alive : true -> This is something which is executed in the Main thread. 线程一还活着 :true->这是在主线程中执行的事情。 There are chances that you may have seen this after seeing the following lines: 在看到以下几行之后,您可能已经看到了这一点:

Two: 5 2:5

Three: 5 三:5

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

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