Introduction to Thread Life Cycle in Java
A thread experiences numerous phases in the life cycle. Such as, a thread comes into the world, started out, runs, and after that passes away. The subsequent diagram explains the complete life cycle of the thread.
- Thread Constructor produces a thread through a new state.
- Calling begin the method in Thread can make it through the runnable state.
- Thread Scheduler concerning Java runs that thread when the processor receives it.
- When the thread goes to a blocked state, it will run yet again because it comes back to a runnable state.
- If the wait method is referred to as thread would go to waiting for the state, it will go to a runnable state soon after it becomes notification throughout the Inform and a notify all method.
- The thread ends once the run method terminates.
What is Thread?
A thread is defined at the Operating System level. And the Java language, as well as all the other languages, uses leverages the service that the Operating System gives. From the developer’s point of view, a thread is a set of instructions that we are going to write our application and execute in a certain way. An application itself can be composed of several threads. Different threads can be executed at the same time. The JVM (Java Virtual Machine) works with several threads. There are threads for the garbage collection. There are threads for the Just in Time Compiler and other technical threads.
States of Thread Life Cycle in Java
Below are the different States of the Thread Life Cycle in Java:
1. New: A new thread starts its life cycle inside the new state. It continues to be with this state before the program begins the thread. Additionally, it is known as a created thread.
2. Runnable: After a recently born thread can begin, the thread turns into runnable. A thread with this state is considered performing their process.
3. Waiting: Occasionally, a thread transitions towards the waiting around the state even though the thread is waiting for another thread to execute an activity. A thread transitions to the runnable state only if an additional thread indicates the waiting thread to keep performing.
4. Timed Waiting: A runnable thread cans easily the particular timed waiting for the state to get a specific interval of the time. A thread with this state transitions returning to the runnable state once that point interval expires or if the event it truly is awaiting happens.
5. Terminated: A runnable thread gets into the terminated state because it accomplishes its task or else terminates.
How to Create a Thread in Java?
The most basic way to create a thread in Java is to use the Runnable Pattern. First, you need to create an instance of the Runnable interface, which is very easy; there is only one method to implement. Then we pass this instance to the constructor of the Thread class. And then, we just call the start() method of this thread object created to launch a new thread that is going to run the task wrapped in our Runnable object.
So first, we create an instance of a Runnable. There is only one method to implement, which is called the run() method. This is the Java 7 pattern to do that, with an instance of an anonymous class. But we can also use a lambda expression to implement a Runnable since there is only one method in the Runnable interface.
Let us create threads on very simple examples.
We are going to see what can go wrong with a race condition that is with unsynchronized code that should be synchronized, and we are going to fix our code using synchronization. This first example is very simple; it’s very basic. It is just about creating a task.
A task is an instance of the Runnable interface, let us call it runnable, and we can implement this interface using a lambda expression. This task is given to a new thread and executed in the context of this thread. So we are just going to print out the name of the thread that is running this task. I am running in… Thread.currentThread() is a static method of the Thread class that returns the thread running the current task. And we just have to call getName() on this thread object to return the name of a thread, then after we create a Thread instance t = new Thread. Passing this runnable as a parameter. So this thread is going to execute this piece of code. And to launch it. t.start() this is the start() method that we need to call. We can also give an explicit name of this Thread using t.setName(“My thread”). And now we can execute this code. Now instead of the call start() method, we call the run() method, and if we run this code, the problem is that the task is correctly executed, but it is not executed in the thread we have created. It is executed in the main thread, which is the thread executing the main method. So this run() method should not be called if we want to launch a new thread.
Methods of Thread Life Cycle in Java
The methods described by simply Thread are presented in Table.
|Data Types||Thread Method Names|
Return this thread’s name
Returns the thread’s priority
Tests if this thread is still running
Waits for this thread to die (terminate)
Whenever this thread was built utilizing an individual Runnable object, which usually Runnable object’s run method is called, this method will do nothing and returns. Whenever thread class can be extended and run() method is over-ridden during sub-class, then an over-ridden run() method is called.
Alterations the name with this thread to become comparable to the argument name.
|sleep(long millis) throws Interrupted/Exception
It causes the presently performing thread to rest for
the required quantity of milliseconds.
|static void||sleep(long millis, int Nanos) throws InterruptedException
It causes the presently performing thread to sleep (cease execution) for the required quantity of milliseconds as well as the particular quantity of nanoseconds.
Triggers these threads to start execution; the Java Virtual Machine calls the run method of that thread.
Triggers the presently thread object to pause and permit additional threads to execute briefly.
Returns a mention of the presently executing thread object.
- Simple to begin using threads, extremely difficult to grasp.
- Designing classes that contain methods that can be thread secure is challenging.
- Read: JavaDoc to get the class java.lang.Thread
This has been a Guide to the Thread life cycle in Java. Here we discuss the basic meaning and different States of the thread life cycle and the creation of java threads. You can also go through our other suggested articles to learn more –