Execution of a thread, after another [closed]

-1

I have two threads, and I need the one to start, when the first one ends. How could I do it? I have tried something, in the second thread, the one that I want to run after the first one, I put thread2.join (); . I do not know if that will work, but I read that it worked.

    
asked by urrutias 10.06.2017 в 19:39
source

1 answer

1

The simplest approach would be to use .join() to wait for the end of its execution before starting the second thread.

The idea is to invoke the .start() of the second thread after calling the .join() of the first thread. That way you guarantee that when you start t2 the execution of run() of t1 has ended.

class MiHilo implements Runnable(){
     @Override
      public void run(){
         // method logic...
     } 
 }

Thread t1 = new Thread(new MiHilo ()); 
t1.start();
t1.join(); 
Thread t2 = new Thread(new MiHilo ());
t2.start();
t2.join();
    
answered by 10.06.2017 в 23:21