在java中反向使用Thread.join()

时间:2012-02-03 17:17:17

标签: java

我知道我可以使用以下内容让线程B在继续之前等待线程A完成:

class A extends Thread{
    public void run(){
        ....
    }
}
class B extends Thread{
    private Thread someThread;
    B(Thread t){
        someThread=t;
    }
    public void run(){
        someThread.join();
        ...//then proceed
    }
}

但是如何在run()的{​​{1}}中调用A,然后等待完成才能继续?也就是说,我想要像

这样的东西
B

6 个答案:

答案 0 :(得分:1)

相同的交易:

class A extends Thread{
    private Thread someThread;
    A(Thread t){
        someThread=t;
    }
    public void run(){
        someThread.start();
        someThread.join();
        // proceed
    }
}
class B extends Thread{
    public void run(){
        ....
    }
}

答案 1 :(得分:1)

如果可能,更简单的解决方案是首先启动B,然后启动b.join();,并在客户端代码中为A执行相同操作:

b.start();
b.join();
a.start();
a.join();

开始一个线程没有任何意义,只能在继续之前启动并加入另一个胎面。

答案 2 :(得分:0)

class A extends Thread{

    private Thread someThread;

    B(Thread t){
        someThread=t;
    }
    public void run(){
        someThread.join()
        //do stuff
    }
}

class B extends Thread{

    public void run(){
        ....
    }
}

答案 3 :(得分:0)

Thread b = new B();
b.start();
b.join();

但你为什么要这样做?线程的想法是并行处理事情。你为什么要开始一个线程并立即等待它完成?只需在A中执行B中的东西。

答案 4 :(得分:0)

您可以使用join()等待任何线程。

final Thread a = Thread.currentThread();
new Thread(new Runnable() {
    public void run() {
        // do something
        a.join();
        // calling thread finished.
    }
 }).start();
 // do something.

答案 5 :(得分:0)

在下面找到如何反向执行线程的示例。

我们可以在这种情况下使用join方法。

    package interview.thread;
     public class RevarseThreadExcute {
    public static void main(String[] args) throws InterruptedException {
   Thread t3 = new Thread(new Runnable() {
    @Override
            public void run() {

                for (int i = 0; i < 10; i++) {
                    System.out.println("Thread 3 =" + i);

                }
            }
        });
        Thread t2 = new Thread(new Runnable() {

            @Override
            public void run() {
               try {
                t3.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                for (int i = 0; i < 10; i++) {
                    System.out.println("Thread 2 =" + i);

                }
            }
        });

        Thread t1 = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    t3.join();
                    t2.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                for (int i = 0; i < 10; i++) {
                    System.out.println("Thread 1 =" + i);

                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
    }


}

outpot:

Thread 3 =0
Thread 3 =1
Thread 3 =2
Thread 3 =3
Thread 3 =4
Thread 3 =5
Thread 3 =6
Thread 3 =7
Thread 3 =8
Thread 3 =9
Thread 2 =0
Thread 2 =1
Thread 2 =2
Thread 2 =3
Thread 2 =4
Thread 2 =5
Thread 2 =6
Thread 2 =7
Thread 2 =8
Thread 2 =9
Thread 1 =0
Thread 1 =1
Thread 1 =2
Thread 1 =3
Thread 1 =4
Thread 1 =5
Thread 1 =6
Thread 1 =7
Thread 1 =8
Thread 1 =9