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.
- When thread would go to a blocked state, it is going to 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 would go to runnable state soon after it will become notification throughout the Inform as well as a notify all method.
- Thread ends once 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 is given by the Operating System. 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 their 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 transition towards the waiting around the state even though the thread is waiting for another thread to execute an activity. A thread transitions returning to the runnable state only if an additional thread indicator the waiting thread to keep performing.
4.8 (3,556 ratings)
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. You need first 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 start() method that we need to call. What we can also do is give an explicit name of this Thread using t.setName(“My thread”). And now we can execute this code. Now instead of 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, after that which usually Runnable object’s run method is called; or else, this method will nothing as well as, returns. Whenever thread class can be extended as well as, 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
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 briefly pause and permit additional threads to execute.
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 what is thread? and different States of the thread life cycle along with the creation of threads in java. You can also go through our other suggested articles to learn more –