Java Multithreading : Thread States and Thread Priority

In a thread lifecycle it can  have one of the  following states mentioned below. I have already explained Thread Life Cycle and basics of thread including Daemon Threads.Now let see inside the thread states and how to achieve them .

  • NEW : A thread has not started yet.
  • RUNNABLE : Thread is running state but it can be in state of waiting.
  • BLOCKED :  Thread is waiting to acquire monitor lock to enter into a synchronized block/method after calling Object.wait()
  • WAITING : A thread is in waiting state due to calling one of the following methods
    • Object.wait() : It causes current thread to wait until it been notified by method  or notifyAll().
    • Object.join() : Waits for current thread to die.
    • LockSupport.park : Disables the current thread for thread scheduling purposes unless the permit is available.
  • TIMED_WAITING : Current thread is waits for another thread for specified time to perform the aciton.
    • Thread.sleep (long timeInMilliSecond) : Makes current thread to cease the execution for specified time.
    • Object.wait (long timeInMilliSecond) :  Causes current thread to wait for specified time until time elapsed or get notified by notify() or notifyAll().
    • Thread.join (long millis) : Current thread waits for specified time to die the thread.
    • LockSupport.parkNanos (long nanoSeconds) : Disables the current thread for thread scheduling purposes, for up to the specified waiting time, unless the permit is available.
    • LockSupport.parkUntil ()
  • TERMINATED : When thread completed its execution.

Also, how to set Thread Priority



WAITING and TIMED_WAITING due to wait() and wait(timeGiven) :

When wait() is called on a Thread, it will keep waiting until notified.

While wait(timeGiven), wait until the timeGiven expired or notified.


BLOCKED : When thread is waiting to enter acquire monitor lock or to enter into synchronized method/block  or to enter into Critical Section

Observe the output, Thread t2 is in blocked status while t1 is executing. Without synchronized block both thread will read original value of  givenVar (try without synchronized block).

Note: Here two threads are working on single object and synchronized block ensuring that only one thread will enter inside the block. This block also known as Critical section.

WAITING and TIMED_WAITING becuase of Thread.join() and Thread.join(givenTime)

threadX.join() —  It makes calling thread to wait for threadXto finish.

threadX.join(1000) — Calling thread will be glued to thread threadX for 1000 miliSeconds and resume after this.

 WAITING due to LockSupport.park() : Javadoc

 Methods park and unpark provide efficient means of blocking and unblocking threads and avoids the the Race condition between one thread is parking another is unparking.

Following is simplest program I code to demonstrate this situation to unpark by other thread.


Setting Thread Priority

You can set priority from 1 (lowest) to 10 (highest) by using thread.setPriority(number) or can use Thread.MAX_PRIORITY, Thread.MIN_PRIORITY. By default thread priority set to 5 that is Thread.NORM_PRIORITY.

Setting priority doesn’t guarantee to execute higher priority thread to run first. It just hint the scheduler that this thread has high priority but it’s depends on scheduler how to queue the threads.

I hope threads states are clear now, Feel free to comment and share.

Leave a Reply

Your email address will not be published. Required fields are marked *