How to run multiple threads sequentially?

Asked

Viewed 1,505 times

1

I have to run a thread x and let the thread y start only when x is done, regardless of how long it will take, and then run z only when y is done. Does anyone know the best way to do this? Thanks in advance.

  • 2

    Why do this?

1 answer

4


It is unusual to find situations where one thread has to expect the other in a serial way. However, these situations exist, and so here goes:

Use the method Thread.join(). This is a class instance method Thread. Thus, a call to t.join();, where t is an instance of Thread, will thread current (Thread.currentThread()) wait for the end of the referenced thread (t).

Here is an example:

public class EsperaThreads {
    public static void main(String[] args) {
        Thread z = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Thread z");
            }
        });
        Thread y = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    z.join();
                    System.out.println("Thread y");
                } catch (InterruptedException e) {
                    System.out.println("Thread y foi interrompida enquanto esperava z");
                }
            }
        });
        Thread x = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    y.join();
                    System.out.println("Thread x");
                } catch (InterruptedException e) {
                    System.out.println("Thread x foi interrompida enquanto esperava y");
                }
            }
        });
        x.start();
        y.start();
        // Se quiser, coloque um Thread.sleep aqui para perceber que x e y não saem na frente de z.
        z.start();
    }
}

Note that the method join() can launch a InterruptedException. This exception is thrown in a case as for example when the thread y is standing in the z.join() and some other thread gives a y.interrupt(), signaling that the thread y should give up waiting. And because of this, in the catch you do the treatment in case of "another thread sent this from here give up waiting". This is useful to signal situations where you want the cancellation or abort of the action being performed.

The method join() also has two other versions overloaded: join(int millis) and join(int millis, int nanos). These versions wait for a maximum time before giving up and moving on. If you pass zero as a parameter, they will wait as long as necessary (or forever if the expected thread never ends).

  • In android you can use Java 8?

  • @ramaral Not easy, yet. Anyway, if you want, I downgrade it to Java 7 easy.

  • @ramaral I did the downgrade.

  • Not by me. However I noticed now that my comment does not make sense because the question does not refer Android. I was only led to this because AP questions are usually in relation to Android

  • That’s exactly what I needed, thank you Stafusa.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.