Java 6 Thread States and Life Cycle.

Ref:  Java 6 Thread States and Life Cycle

This is an example of UML protocol state machine diagram showing thread states and thread life cycle for the Thread class in Java 6.

Thread is a lightweight process, the smallest unit of scheduled execution. Instance of the Thread class in Java 6 could be in one of the following states:

  • new,
  • runnable,
  • timed waiting,
  • waiting,
  • blocked,
  • terminated.

These states are Java Virtual Machine (JVM) states reported by JVM to Java programs. At any given point in time thread could be in only one state.

  Java 6 Thread States and Life Cycle.

Protocol state machine example - Thread states and life cycle in Java 6

New is the thread state for a thread which was created but has not yet started.

At the lower operating system (OS) level, JVM’s runnable state could be considered as a composite state with two substates. When a thread transitions to the runnable JVM state, the thread first goes into the ready substate. Thread scheduling decides when the thread could actually start, proceed or be suspended. Thread.yield() is explicit recommendation to thread scheduler to pause the currently executing thread to allow some other thread to execute.

A thread in the runnable state is executing from the JVM point of view but in fact it may be waiting for some resources from the operating system.

Timed waiting is a thread state for a thread waiting with a specified waiting time. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting time:

  • Thread.sleep(sleeptime)
  • Object.wait(timeout)
  • Thread.join(timeout)
  • LockSupport.parkNanos(timeout)
  • LockSupport.parkUntil(timeout)

A thread is in the waiting state due to the calling one of the following methods without timeout:

  • Object.wait()
  • Thread.join()
  • LockSupport.park()

Note, that thread in the waiting state is waiting for another thread to perform a particular action. For example, a thread that has called Object.wait() on an object is waiting for another thread to call Object.notify() or Object.notifyAll() on that object. A thread that has called Thread.join() is waiting for a specified thread to terminate. It means that waiting state could be made a composite state with states corresponding to these specific conditions.

Thread is in the blocked state while waiting for the monitor lock to enter a synchronized block or method or to reenter a synchronized block or method after calling Object.wait().

A synchronized statement or method acquires a mutual-exclusion lock on behalf of the executing thread, executes a block or method, and then releases the lock. While the executing thread owns the lock, no other thread may acquire the lock and is blocked waiting for the lock.

After thread has completed execution of run() method, it is moved into terminated state.

Java 6 Thread States and Life Cycle.,布布扣,bubuko.com

Java 6 Thread States and Life Cycle.

上一篇:分库分表落地解决方案


下一篇:【python】将01组成的密码用ASCII编码成字符串