Creating threads, running them and their states

 Creating a Thread

A thread consists of two aspects, the main thread (which is the thread that is run when the program starts) and the slave thread which is a 'product' or result of the main thread and is considered the final thread that completes the execution of the thread.

We are able to create threads making use of two approaches: 
• Implementing a Runnable interface
• Extending the Thread class

The difference between implementing Runnable interface and extending the Thread is that when we extend the Thread class, we are unable to extend any other class, even if we need to, where as implementing Runnable saves space for our class to extend any other class at any time. 

Implementing java.lang.Thread class













Output



Implementing java.lang.Runnable interface






                                                                        












                                                   

                                                                            Output 

What if we do not override the thread run() method?

If the thread .run() method is not override, no particular error will flash and the method will be run an empty Thread class, resulting in no output for the thread. It be as though the thread does not exist.

A big benefit to implementing java.lang.Runnable thread forces the developer to override the run() method.

 

States of a Thread

Once our threads have been structured to complete a process, we are able to identify where in the process we are by taking a look into state of the threads. These states are more particularly useful when trying to diagnose process crashes or issues, by identifying the thread that has impacted the process by its state. 


6 States of Threads

  • New
  • Runnable
  • Blocked
  • Waiting
  • Timed Waiting
  • Terminated
Lets get a little visual of what the life cycle of a thread looks like to better understand how threads play their roles in completing a process.

Threads change states by means of a scheduler which reacts when conditions in the process have been met. 

Let's take a look at what each of these states represent:  

NEW This is described as a thread that has just been written but never run or has not yet started

RUNNABLE  When a thread is ready to be run is moved into this state

BLOCKED ➤ A thread moves into this state IF the thread attempts to perform a task that could not be completed at that given time (for example, when a program is waiting for input from a user) at which point, the thread stops making use of the CPU

WAITING ➤ A thread is in this state when it is waiting for another particular thread to complete its given task. Once the notify() or notifyAll() method is called in the other thread, this thread moves back into a runnable state.

TIMED WAITING ➤ This thread calls the methods sleep, wait or join with a time parameter. The thread will remain in this state until timeout (the parameter time has been reached) or when the thread receives a notification that the other thread had successfully completed its task. It then moves back into a runnable state

TERMINATED ➤ The thread is complete. This can happen once the entire process was successfully completed OR when one of the threads run into an error, and the thread is shut down.

Thank you to javatpoint.com and geeksforgeeks.com for resources and information, and you can read up more on this topic by clicking their links.

Comments

Popular posts from this blog