All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Thread States In Java

Last Updated : Mar 11, 2024

Thread States In Java

In this article we will show you the solution of thread states in java, in java, there are various thread states such as NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.

In NEW state, a thread is initiliazed but not start for executing or not yet eligible to the run.

In RUNNABLE state, when the thread is ready to go, but currently is not in executing.

In BLOCKED state, when thread enter a synchronized block but another thread hold the lock currently.

In WAITING state, a thread occurs when a thread calling wait() method or or wait when the condition is satisfied

In TIMED_WAITING state, a thread waits for the specific amount of time for another thread to performing an action.

In TERMINATED state, a thread completes the execution process then it terminates.

We can use various thread class that can manage threads in java such as start() method, run() method, sleep() method, join() method, interrupt() method, isAlive() method etc.

In start() method , thread class starting the execution of the thread, In run() method, when a program of java is successfully started then run() method automatically calls.

In sleep() method, a thread is in sleep or stop in some amount of time.

In join() method, it waits when will be the other thread dead.In interrupt() method,it interrupt the thread execution.

In isAlive() method, it checks wheather the thread is active or not.

Step By Step Guide On Thread States In Java :-

Different thread states in java
public class Talkerscode {
    public static void main(String[] args) {
        Thread thread = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(" Thread state should be in:" + Thread.currentThread().getState());
        });
        thread.start();
    }
}
  1. Firstly, we can declare a class named “Talkerscode” as public.
  2. Secondly, we can create a instance class marked as static and void which takes an array of string and pass command line argument
  3. Then, we can create a variable named thread of Thread type.
  4. In try expression, we can use sleep() method to stop the thread execution in some amount of time. In this code, thread can sleep for 1000milliseconds
  5. In catch expression, exception is catch and we can use e.printStackTrace() method to print the stack trace
  6. After using sleep() method, then we can use Thread.currentThread() to update the current thread and use getState() method to get the state of current thread
  7. Then we can use start() method to start the execution of thread.

Conclusion :-

In conclusion, we can easily learned thread states in jave. We learned various states in thread such as NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAIT, TERMINATED.

And we learned thread method such as start() method, run() method, sleep() method, join() method, interrupt() method, isAlive() method.

All these states are really helpful to start, run, stop the thread execution in java. These states can easily manage threads.

I hope this article on thread states in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪