Sitemap

Threads in Java — Part 6 (Thread Join and Thread Life Cycle)

3 min readMay 19, 2021

Let’s discuss about the Thread Life Cycle. It’s really a seems as our lives. Follow figure shows about the main behaviors of threads during its life cycle.

  • When the thread is created, it goes to New state.
  • When the thread is started, it goes to the Ready/Runnable state.
  • When the thread scheduler is scheduled a particular thread, it goes to the Running state.
  • When the thread is finished its execution, then it goes to Dead state.
  • Also, this ready and running states can be changed back and forth.

Thread Join

The Thread class consumes join() in order to wait one thread until another one finishes its works. Let’s think about in real world example.

In my workplace, I have finished my works by 5.00 P.M but my friend still working. So, I decided to wait until my friend finished her works. So, I went and tell her I’ll be wait until you finished your works and then we can leave. If not I can tell her I’ll will stay for 30 minutes until she finishes her works. That means I tell her about my waiting within particular time frame. However remember that in both cases I decided to wait until my friend comes.

  • Like wise, let’s say we have 2 thread called T1 and T2.
  • Then T1 decided to wait until T2 completes its execution and then continue its process.
  • T1 called join() on T2 by using different ways as following.

1. T2.Join(),

2. T2.Join(long milli_seconds): In here you can specifies T1 waiting with milli seconds (ms)

3. T2.Join(long milli_seconds, int nano_seconds): In here you can specify T1 waiting with ms and nano seconds (ns).

Below discuss about what is really happening to the T1 thread and its state changes.

  • When T1 run it goes to Ready state.
  • When execute, it goes to running state.
  • Once T1 decides to wait until T2 finishes, it goes to waiting state.
  • Then after T2 finishes, T2 can’t directly go back to the running state from waiting state but want to go through ready state and then running. This is a rule in thread.
  • There are 3 ways that T1 can go to the waiting state to ready state to finish its execution.

1.When T2 finished its execution.

2.When time-out (Think about in our real world example which says I’ll be wait until 30 minutes for her).

3.When T1 got interrupted (Think I got call from my family about emergence situation and I must go there without wait until my friend comes)

Now, let’s discuss about this with our regular program. Our main thread waits until child thread completed. Both child and main thread print numbers up to 10. Then main thread called join() and look what will happen.

So, you can see that child thread finished execution before main thread.

Hope you get the clear thoughts about thread life cycle and joining. Let’s discuss about the 3 types of methods used in thread in my next article related with thread join.

Stay Safe !!!

References:

https://www.youtube.com/watch?v=-e0sIPSVSL0&list=PLD-mYtebG3X99o6vJ3uR5P6UcH3MQSWBH&index=5

--

--

No responses yet