简体   繁体   中英

reverse using of Thread.join() in java

I know that I can use the following to have thread B waiting for thread A to finish before proceeding:

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
    }
}

But how can I do this in the run() of A to call B and then wait it finish before proceeding? That is, I want something like

class A extends Thread{
    private Thread someThread;
    B(Thread t){
        someThread=t;
    }
    public void run(){
        //*how to start B and wait it finish?
        ...//then proceed
    }
}
class B extends Thread{
    public void run(){
        ....
    }
}

Same deal:

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(){
        ....
    }
}

If possible, a simpler solution would be to start B first, then b.join(); , and do the same for A in the client code:

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

There is no sense in starting a thread for it to just fire up and join on another tread before proceeding.

Find below example for how to reverse execute the thread.?

We can use join method for this case.

    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
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(){
        ....
    }
}
Thread b = new B();
b.start();
b.join();

But why would you want to? The idea of threads is to do things in parallel. Why would you start a thread and immediately wait for it to finish? Just execute the stuff from B in place in A.

You can use join() to wait for any thread.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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