solitaryclown

Java线程的6种状态之间的转换过程

2021-12-19
solitaryclown

1. java线程状态转换

1.1. java线程的6种状态

在java.lang.Thread中定义了线程的6种状态:NEWRUNNABLEWAITINGTIMED_WAITINGBLOCKEDTERMINATED

public enum State {
        /**
         * Thread state for a thread which has not yet started.
         */
        NEW,

        /**
         * Thread state for a runnable thread.  A thread in the runnable
         * state is executing in the Java virtual machine but it may
         * be waiting for other resources from the operating system
         * such as processor.
         */
        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * A thread in the blocked state is waiting for a monitor lock
         * to enter a synchronized block/method or
         * reenter a synchronized block/method after calling
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * A thread is in the waiting state due to calling one of the
         * following methods:
         * <ul>
         *   <li>{@link Object#wait() Object.wait} with no timeout</li>
         *   <li>{@link #join() Thread.join} with no timeout</li>
         *   <li>{@link LockSupport#park() LockSupport.park}</li>
         * </ul>
         *
         * <p>A thread in the waiting state is waiting for another thread to
         * perform a particular action.
         *
         * For example, a thread that has called <tt>Object.wait()</tt>
         * on an object is waiting for another thread to call
         * <tt>Object.notify()</tt> or <tt>Object.notifyAll()</tt> on
         * that object. A thread that has called <tt>Thread.join()</tt>
         * is waiting for a specified thread to terminate.
         */
        WAITING,

        /**
         * Thread state for a waiting thread 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:
         * <ul>
         *   <li>{@link #sleep Thread.sleep}</li>
         *   <li>{@link Object#wait(long) Object.wait} with timeout</li>
         *   <li>{@link #join(long) Thread.join} with timeout</li>
         *   <li>{@link LockSupport#parkNanos LockSupport.parkNanos}</li>
         *   <li>{@link LockSupport#parkUntil LockSupport.parkUntil}</li>
         * </ul>
         */
        TIMED_WAITING,

        /**
         * Thread state for a terminated thread.
         * The thread has completed execution.
         */
        TERMINATED;
    }

1.2. 线程状态的转换

1.2.1. NEW———>RUNNABLE

线程调用start()

1.2.2. RUNNABLE<———>WAITING

  1. wait-notify
    • 线程获得对象锁,调用obj.wait(),从runnable变为waiting。
    • 线程被notify()或者notifyAll()或者interrupt()
      • 线程竞争到对象锁,从waiting变为runnable
      • 否则,从waiting变为blocked
  2. join
    • 当前线程调用t.join(),当前线程从runnable变为waiting
      • 线程t运行结束或者当前线程被interrupt(),当前线程从waiting变为runnable
  3. park-unpark
    • 当前线程调用LockSupport.park(),从runnable变为waiting
      • 线程调用LockSupport.unpark(t),线程t从waiting变为runnable
      • 线程调用t.interrupt(),线程t从waiting变为runnable

1.2.3. RUNNABLE<———>TIMED_WAITING

  1. wait-notify
    • 线程获得对象锁,调用obj.wait(long n),从runnable变为timed_waiting。
    • 线程等待时间超过n毫秒或者notify()或者notifyAll()或者interrupt()
      • 线程竞争到对象锁,从timed_waiting变为runnable
      • 否则,从timed_waiting变为blocked
  2. join
    • 当前线程调用t.join(long n),当前线程从runnable变为timed_waiting
    • 线程等待时间超过n毫秒或者线程t运行结束或者当前线程被interrupt(),当前线程从timed_waiting变为runnable
  3. park-unpark
    • 当前线程调用LockSupport.parkNanos(long nanos)LockSupport.parkUntil(long millis),从runnable变为timed_waiting
    • 线程调用LockSupport.unpark(t),线程t从timed_waiting变为runnable
    • 线程调用t.interrupt(),线程t从timed_waiting变为runnable

1.2.4. RUNNABLE<———>BLOCKED

  • 线程进入synchronized代码块之前没有竞争到锁,从runnable变为blocked
  • 等待锁的线程重新竞争到锁,从blocked变为runnable

1.2.5. RUNNABLE———>TERMINATED

线程执行完毕


上一篇 Park()&unpark()

Comments

Content