Interview Questions

Output question 2.

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

62. Output question 2.

class MyRunnable implements Runnable{
public void run(){
for(int i=0;i<3;i++){
System.out.println("i="+i+"
,ThreadName="+Thread.currentThread().getName());
}
}
}
public class MyClass {
public static void main(String...args) throws InterruptedException{
System.out.println("In main() method");
MyRunnable runnable=new MyRunnable();
Thread thread1=new Thread(runnable);
Thread thread2=new Thread(runnable);
thread1.start();
thread1.join();
thread2.start();
thread2.join();
System.out.println("end main() method");
}
}


Answer.

We use join() method to ensure all threads that started from main must end in order in which they started and also main should end in last. In other words join() method waited for this thread to die.

/*OUTPUT
In main() method
i=0 ,ThreadName=Thread-0
i=1 ,ThreadName=Thread-0
i=2 ,ThreadName=Thread-0
i=0 ,ThreadName=Thread-1
i=1 ,ThreadName=Thread-1
i=2 ,ThreadName=Thread-1
end main() method
*/

(Continued on next question...)

Other Interview Questions