0

Suppose I have the following class below, how can I force the three threads to be executed in order, one after the other successively? (waiting for each other to be terminated)

public class MyRunnable implements Runnable{

    @Override
    public void run() {

        System.out.println("Thread 1 :First Thread started");
    }

    public static Runnable delay(){
        Runnable r = new Runnable(){

            @Override
            public void run() {  // running state

                System.out.println("Thread 2: loading second thread..");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 2: System loaded");
            }

        }; // finished state
        return r;
    }

    public static Runnable waiting(){
        Runnable r = new Runnable(){

            @Override
            public void run() { // running state

                System.out.println("Thread 3: waiting..");
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread 3: OK");

            }

        }; // finished state
        return r;
    }


    public static void main(String[] args) throws InterruptedException{

       Thread thread1 = new Thread(new MyRunnable());      
       Thread thread2 = new Thread(delay()); 
       Thread thread3 = new Thread(waiting()); // (initial state)

       thread1.start();
       thread2.start();
       thread3.start();

    }


}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
johnny manolly
  • 95
  • 1
  • 10
  • 8
    The question is why? If you want operations to happen in sequence, you don't need threads, just write the operations one after the other in the same method. – RealSkeptic Jul 12 '15 at 09:48
  • Actually, there are revealed advantages too,don't know why... Check this answer of mine ---> https://stackoverflow.com/questions/30885037/multithreading-using-blocking-io-corrupts-file-in-java/30919133#30919133 @RealSkeptic – Am_I_Helpful Jul 12 '15 at 10:25
  • @shekharsuman Something more serious is needed as evidence. The actual benchmark code, for example. And a simplified example that can be ran anywhere. – RealSkeptic Jul 12 '15 at 10:37
  • Well, you could try the same code, and then judge it better. BTW, I had performed it in a neutral environment,with no biasing at all at any level. And, I also agree that some serious look is needed there... @RealSkeptic – Am_I_Helpful Jul 12 '15 at 10:40

2 Answers2

2

Yes there are, but why do you want to do it?

public static void main(String[] args) throws InterruptedException{

    Thread thread1 = new Thread(new MyRunnable());      
    Thread thread2 = new Thread(delay()); 
    Thread thread3 = new Thread(waiting()); // (initial state)

    thread1.start();
    thread1.join();
    thread2.start();
    thread2.join();
    thread3.start();
    thread3.join();
}

Other way (without threads):

public static void main(String[] args) throws InterruptedException{

    new MyRunnable().run();
    delay().run(); 
    waiting().run();
} 

Your code do this:

Main thread      thread-1        thread-2       thread-3
    V
    |
    + . . . . . . > V
    + . . . . . . . | . . . . . . > V
    + . . . . . . . | . . . . . . . | . . . . . . > V
    X               |               |               |
                    X               |               |
                                    |               |
                                    X               |
                                                    |
                                                    X    

You asked for this (it no make sense because threads can parallelize tasks, and you don't want to parallelize them!):

Main thread      thread-1        thread-2       thread-3
    V
    |
    + . . . . . . > V
    |               |
    |<--------------X
    + . . . . . . . . . . . . . . > V
    |                               |
    |                               |
    |                               |
    |                               |
    |<------------------------------X
    + . . . . . . . . . . . . . . . . . . . . . . > V
    |                                               |
    |                                               |
    |                                               |
    |                                               |
    |                                               |
    |                                               |
    |<----------------------------------------------X
    X
David Pérez Cabrera
  • 4,960
  • 2
  • 23
  • 37
  • I'm totally new with threads and i'm developing a mutli-threading application, the question above is just a sample example to understand how it works, thanks for your answer! – johnny manolly Jul 12 '15 at 10:11
0

Put thread.join ()after you start the thread. This will wait for the thread to return before the next thread starts.

Luke Melaia
  • 1,470
  • 14
  • 22