how to run threads after other threads finished, let say i have 3 java class (Cls1 and Cls2 implements runnable, and I use sleep to know which statements are run first), this is my code :
public class Master {
@SuppressWarnings("unused")
public static void main(String[] args) {
//loop1
for(int i=1; i<=2; i++) {
Cls1 c1 = new Cls1();
}
//Here i want to wait until the thread loop1 is finished, what to do?
//loop2
for(int j=1; j<=2; j++) {
Cls2 c2 = new Cls2();
}
}
}
public class Cls1 implements Runnable{
Thread myThread;
Cls1() {
myThread = new Thread(this, "");
myThread.start();
}
@Override
public void run() {
System.out.println("hello1");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello2");
}
}
public class Cls2 implements Runnable{
Thread myThread;
Cls2() {
myThread = new Thread(this, "");
myThread.start();
}
@Override
public void run() {
System.out.println("hello3");
TimeUnit.SECONDS.sleep(3);
System.out.println("hello4");
}
}
And this is output my code : hello1 hello1 hello3 hello3 hello2 hello2 hello4 hello4
And this is the output I expect: hello1 hello1 hello2 hello2 hello3 hello3 hello4 hello4
What should I do ?