Java线程状态(Thread.State)

Posted dddjp

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java线程状态(Thread.State)相关的知识,希望对你有一定的参考价值。

java.lang.Thread.State

成员内部类

NEW:还未启动

  Thread state for a thread which has not yet started.

RUNNABLE:正在jvm中运行,但是可能正在等待操作系统的其他资源

  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.

BLOCKED:受阻塞,并且正在等待监视器锁

  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}.

WAITING:处于等待状态的线程,正在等待另一个线程执行特定的操作

  Thread state for a waiting thread.

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

    {@link Object#wait() Object.wait} with no timeout

    {@link #join() Thread.join} with no timeout

    {@link LockSupport#park() LockSupport.park}

  A thread in the waiting state is waiting for another thread to perform a particular action.

  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.

TIMED_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:

    {@link #sleep Thread.sleep}

    {@link Object#wait(long) Object.wait} with timeout

    {@link #join(long) Thread.join} with timeout

    {@link LockSupport#parkNanos LockSupport.parkNanos}

    {@link LockSupport#parkUntil LockSupport.parkUntil}

TERMINATED

  Thread state for a terminated thread.

  The thread has completed execution.

  

Thread start()

     sleep() sleep(?)

        join()

Object wait() notify() notifyAll()

   wait(?)



 

 

新建(new)

可运行(Runnable)

阻塞(block)

无限期等待(waiting)

限期等待(TIMED_WAITING)

结束(terminated)

 

新创建一个线程,线程处于新建状态(new)

  使用重载的new Thread()创建一个新的线程

 

调用线程的start()方法,线程进入可运行状态(Runnable)

  Java线程中的Runnable状态包括操作系统线程的running和ready状态,处于就绪状态的线程一旦获得CPU使用权,进入运行状态

  即处于Runnable状态的线程可能正在运行,也可能正在等待CPU分配时间片

  调用Thread类的start()方法之后  

 

一个线程处于等待(waiting)状态,表示该线程正在等待其它线程执行特定的操作

  例如,一个线程调用了Object类的空参的wait方法,表示该线程等待其它线程调用该对象的notify或者notifyAll方法

        一个线程调用了Thread类的空参的join方法,表示该线程在等待一个特定的线程终止

  何时进入waiting状态:

    调用Object类空参的wait()

    调用Thread类空参的join()

 

一个线程在执行过程中,想获得某一资源的锁,但是该资源又被其它线程占用,此时,当前线程处于阻塞状态(例如,进入一个同步方法或者同步代码块,要先获得锁)

  阻塞和等待的不同是,阻塞状态是在等待一个排它锁

              等待状态实在等待一个事件的发生,如等待时间到,唤醒方法被调用

 

一个线程处于超时等待状态(timed_waiting),和waiting不同的是,将在特定的时间内自行返回

  何时进入timed_waiting状态:

    调用Object类有参的wait()

    调用Thread类有参的join()

    调用Thread类的sleep方法(sleep方法没有空参的重载形式)

 

一个线程执行完毕,进入终止状态(terminated)

  线程执行完毕

  线程在执行过程中抛出异常

 

以上是关于Java线程状态(Thread.State)的主要内容,如果未能解决你的问题,请参考以下文章

线程处于 WAITING 状态:java.lang.Thread.State: WAITING (parking) at sun.misc.Unsafe.park(Native Method)

Java线程 — 线程的通信 — 线程的生命周期和状态概述

日常小结-java线程状态的转移

日常小结-java线程状态的转移

shell分析线程堆栈

Java线程