Posts

Showing posts from April, 2017

Java - Thread join

In this code, what does the two joins and break mean?  t1.join()  causes  t2  to stop until  t1 terminates? Thread t1 = new Thread ( new EventThread ( "e1" )); t1 . start (); Thread t2 = new Thread ( new EventThread ( "e2" )); t2 . start (); while ( true ) { try { t1 . join (); t2 . join (); break ; } catch ( InterruptedException e ) { e . printStackTrace (); } } There is a thread that is running your example code which is probably the  main thread . The main thread creates and starts the  t1  and  t2  threads. The two threads start running in parallel. The main thread calls  t1.join()  to wait for the  t1  thread to finish. The  t1  thread completes and the  t1.join()  method returns in the main thread. The main thread calls  t2.join()  to wait for the  t2  thread to finish. The  t2  thread complet...