JAVA多线程--线程的状态(一)

时间:2022-02-11 07:23:04

线程是什么?

线程是操作系统能够调用的最小单位。

Java中线程共有几种状态呢?

通过查询JDK中的Thread.class中可以看到,java中的线程状态分别是:

  1. NEW 初始态,线程被创建成功,但是还没有被运行。

  2. RUNNABLE 可运行状态,此时线程已经被JVM加载,但是是否执行要等CPU的调度。

  3. BLOCKED 阻塞状态 ,此时线程在被阻塞,等待监视器锁的释放。

  4. WAITING 等待状态,等待其他线程来唤醒。

  5. TIMED_WAITING 有时限的等待状态,也是在等待其他线程的唤醒。

  6. TERMINATED 终止状态,代表线程声明周期的结束。具体可以参照下面的英文解释,可以加深理解。

 /**
      * A thread state. A thread can be in one of the following states: 
      * #NEW# A thread  that has not yet started is in this state. 
      * #RUNNABLE# A thread executing in  the Java virtual machine is in this state.
      * #BLOCKED# A thread that is blocked waiting for a monitor lock is in this state. 
      * #WAITING# A thread that is  waiting indefinitely for another thread to perform a particular action is in  this state. 
      * #TIMED_WAITING# A thread that is waiting for another thread to perform an action for up to a specified waiting time is in this state.
      * #TERMINATED#  A thread that has exited is in this state.
      */
     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;
     }

 

线程中状态的转换

当使用NEW关键字创新线程时,线程的状态为NEW,当我们调用该线程的start()方法时,此时线程的状态是RUNNABLE,当该线程在执行过程中尝试获取锁的却获取不到的时候,线程进入BLOCKED状态,当线程阻塞一段时间最终获取到锁死时,线程就继续进入RUNNABLE状态,当线程执行Thread.sleep()方法时,线程进入WAITING状态,当线程执行Thread.sleep(500L)时,此时线程的状态为TIMED_WAITING,当线程执行完毕时,线程的状态为TERMINATED状态。