即使在 Java 中使用 Thread.join() 后 Main 也不等待线程
Main not waiting for threads even after using Thread.join() in Java
join() 应该让 main 函数等待所有线程完成执行,但 main 在 Thread-1
和 Thread-2
完成执行之前打印 completed
。
我无法在代码中找到错误。哪里错了?
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
ThreadDemo(String name) {
threadName = name;
}
public void run() {
System.out.println("Thread " + threadName + " exiting.");
}
public void start() {
if (t == null) {
t = new Thread (this, threadName);
t.start();
}
}
}
public class Problem2 {
public static void main(String[] args) {
ThreadDemo T1 = new ThreadDemo("Thread-1");
ThreadDemo T2 = new ThreadDemo("Thread-2");
T1.start();
T2.start();
try {
T1.join();
T2.join();
} catch (InterruptedException e) {
System.out.println("ERROR!");
}
System.out.println("completed");
}
}
输出
completed
Thread Thread-2 exiting.
Thread Thread-1 exiting.
您正在加入 ThreadDemo 实例。但是你不是 运行 这些实例作为线程。您覆盖的 start
方法创建并启动另一个线程。
你的代码非常复杂。您同时使用继承和委托,并且覆盖方法并破坏它们的契约:start() 应该作为线程启动 this
,而不是创建和启动另一个线程。
它应该是这样的:
class ThreadDemo implements Runnable {
private String threadName;
ThreadDemo(String name) {
this.threadName = name;
}
@Override
public void run() {
System.out.println("Thread " + threadName + " exiting.");
}
}
public class Problem2 {
public static void main(String[] args) {
ThreadDemo runnable1 = new ThreadDemo("Thread-1");
ThreadDemo runnable2 = new ThreadDemo("Thread-2");
Thread t1 = new Thread(runnable1);
Thread t2 = new Thread(runnable2);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println("ERROR!");
}
System.out.println("completed");
}
}
join() 应该让 main 函数等待所有线程完成执行,但 main 在 Thread-1
和 Thread-2
完成执行之前打印 completed
。
我无法在代码中找到错误。哪里错了?
class ThreadDemo extends Thread {
private Thread t;
private String threadName;
ThreadDemo(String name) {
threadName = name;
}
public void run() {
System.out.println("Thread " + threadName + " exiting.");
}
public void start() {
if (t == null) {
t = new Thread (this, threadName);
t.start();
}
}
}
public class Problem2 {
public static void main(String[] args) {
ThreadDemo T1 = new ThreadDemo("Thread-1");
ThreadDemo T2 = new ThreadDemo("Thread-2");
T1.start();
T2.start();
try {
T1.join();
T2.join();
} catch (InterruptedException e) {
System.out.println("ERROR!");
}
System.out.println("completed");
}
}
输出
completed
Thread Thread-2 exiting.
Thread Thread-1 exiting.
您正在加入 ThreadDemo 实例。但是你不是 运行 这些实例作为线程。您覆盖的 start
方法创建并启动另一个线程。
你的代码非常复杂。您同时使用继承和委托,并且覆盖方法并破坏它们的契约:start() 应该作为线程启动 this
,而不是创建和启动另一个线程。
它应该是这样的:
class ThreadDemo implements Runnable {
private String threadName;
ThreadDemo(String name) {
this.threadName = name;
}
@Override
public void run() {
System.out.println("Thread " + threadName + " exiting.");
}
}
public class Problem2 {
public static void main(String[] args) {
ThreadDemo runnable1 = new ThreadDemo("Thread-1");
ThreadDemo runnable2 = new ThreadDemo("Thread-2");
Thread t1 = new Thread(runnable1);
Thread t2 = new Thread(runnable2);
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
System.out.println("ERROR!");
}
System.out.println("completed");
}
}