Java线程状态详解

当我们使用jstack查看java线程的运行情况时,会看到很多的线程的状态,这时候如果不了解线程状态之间的关系就很难做出分析。

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

如上面的代码所示,java的thread将线程的状态定义了6中状态,下面我们来逐一介绍
NEW
实现Runnable接口和继承Thread可以得到一个线程类,new一个实例出来,线程就进入了初始状态。
RUNNABLE
RUNNABLE可以分为running 和ready两种状态,其中线程调用start方法后进入ready状态,当获得cpu时间片后进入running状态。
BLOCK
线程阻塞于锁的状态,阻塞在进入synchronized关键字修饰的方法或代码块(获取锁)时的状态。当线程长时间处于此状态时我们要检查是不是出现了死锁。
WAITING
从上面不难看出线程在执行过程中调用了Object.wait(),Thread.join,Locksupported.parkUtil()等方法时会进入waiting状态。
处于这种状态的线程不会被分配CPU执行时间,它们要等待被显式地唤醒,否则会处于无限期等待的状态。再次调用notify(),notifyAll(),Locksupported.unpark()等方法时,又会重新进入运行时状态。
TIMED_WAITING
当线程调用Thread.sleep(long) Object.wait(long), join(long) LockSupport.parkNanos ockSupport.parkUntil 等方法时,线程进入此状态。处于这种状态线程不会被分配CPU时间,但是也无需主动唤醒,时间到了他们会进入就绪状态。
TERMINATED
线程run方法执行完成或是mian方法执行完后,我们就认为线程结束,线程处于此状态将不能在激活。

下面的图很好的说明了线程的运行关系
线程状态转换图

https://www.jianshu.com/p/ec94ed32895f

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值