即使在Java中使用Thread.join()之后,主要还没有等待线程

时间:2021-08-27 00:25:11

join() is supposed to make main function wait until all the threads complete execution, but main is printing completed before Thread-1 and Thread-2 completes execution. I am unable to find error in the code. where's the mistake?

join()应该使main函数等到所有线程完成执行,但main在Thread-1和Thread-2完成执行之前打印完成。我无法在代码中找到错误。哪里出错了?

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");
    }
}

Output

completed
Thread Thread-2 exiting.
Thread Thread-1 exiting.

1 个解决方案

#1


3  

You're joining on the ThreadDemo instances. But you're not running these instances as threads. Your overridden start method creates and starts another Thread.

您正在加入ThreadDemo实例。但是你没有将这些实例作为线程运行。您重写的start方法创建并启动另一个Thread。

Your code is extremely convoluted. You use both inheritance and delegation, and you override methods and break their contract: start() is supposed to start this as a thread, not create and start another thread.

您的代码非常错综复杂。你使用继承和委托,你重写方法并打破他们的合同:start()应该作为一个线程启动它,而不是创建和启动另一个线程。

Here's what it should look like:

这是它应该是什么样子:

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");
    }
}

#1


3  

You're joining on the ThreadDemo instances. But you're not running these instances as threads. Your overridden start method creates and starts another Thread.

您正在加入ThreadDemo实例。但是你没有将这些实例作为线程运行。您重写的start方法创建并启动另一个Thread。

Your code is extremely convoluted. You use both inheritance and delegation, and you override methods and break their contract: start() is supposed to start this as a thread, not create and start another thread.

您的代码非常错综复杂。你使用继承和委托,你重写方法并打破他们的合同:start()应该作为一个线程启动它,而不是创建和启动另一个线程。

Here's what it should look like:

这是它应该是什么样子:

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");
    }
}