States of thread in Java

Threads can be in one of the following states:

  • New. After the thread is instantiated, it is in the New state until the start() method is called. In this state, the thread is not considered alive.
  • Runnable. A thread is transitioned to the Runnable state when the start() method is called. A thread can also enter this state from the Running state or from the Blocked state. When a thread is in this state, it is considered alive.
  • Running. A thread transitions from the Runnable state to the Running state when the Thread Scheduler selects it as currently running.
  • Alive, but not runnable. The thread may be alive, but not runnable for several reasons:
    • Waiting. The thread enters the Waiting state by calling the wait() method. Calling notify() or notifyAll() can move a thread from the Waiting state to the Runnable state.
    • Sleeping. The sleep() method puts the thread into a Sleeping state for a specified amount of time in milliseconds.
    • Blocked. A thread can enter this state, waiting for a resource, such as I/O, or because another object is locked. In this case, the thread transitions to the Runnable state when the resource becomes available.
    • Dead. A thread is considered dead when its run() method is completely executed. A dead thread cannot transition to any other state, even if its start() method is called.

Is it possible to create new instances of a class while a static synchronized method is running?

Yes, you can create new instances of the class, since static fields do not belong to class instances.

Why would a private mutex be needed?

The object for synchronization is made private so that third-party code cannot synchronize to it and accidentally acquire a deadlock.


Read also:


Comments

Popular posts from this blog

Methods for reading XML in Java

XML, well-formed XML and valid XML

ArrayList and LinkedList in Java, memory usage and speed