gpt4 book ai didi

Java线程运行顺序

转载 作者:行者123 更新时间:2023-12-02 10:27:15 25 4
gpt4 key购买 nike

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

最佳答案

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

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

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

线程一处于 Activity 状态:true -> 这是在主线程中执行的操作。您可能在看到以下几行后就看到了这一点:

二:5

三:5

关于Java线程运行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53861238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com