Thread状态

一、Thread中对状态定义的枚举

    /**
     * A thread state.  A thread can be in one of the following states:
     * <ul>
     * <li>{@link #NEW}<br>
     *     A thread that has not yet started is in this state.
     *     </li>
     * <li>{@link #RUNNABLE}<br>
     *     A thread executing in the Java virtual machine is in this state.
     *     </li>
     * <li>{@link #BLOCKED}<br>
     *     A thread that is blocked waiting for a monitor lock
     *     is in this state.
     *     </li>
     * <li>{@link #WAITING}<br>
     *     A thread that is waiting indefinitely for another thread to
     *     perform a particular action is in this state.
     *     </li>
     * <li>{@link #TIMED_WAITING}<br>
     *     A thread that is waiting for another thread to perform an action
     *     for up to a specified waiting time is in this state.
     *     </li>
     * <li>{@link #TERMINATED}<br>
     *     A thread that has exited is in this state.
     *     </li>
     * </ul>
     *
     * <p>
     * A thread can be in only one state at a given point in time.
     * These states are virtual machine states which do not reflect
     * any operating system thread states.
     *
     * @since   1.5
     * @see #getState
     */
    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;
    } 

二、状态解释

<p>NEW:线程被创建,还没有调用start()方法。
<p>RUNNABLE:线程调用start()方法后的状态,这个时候可能还没有真正的运行,有可能还在等待系统资 源,可能还没获得CPU。
<p>BLOCKED: 线程处于阻塞状态,等待锁释放。等待进入synchronized方法或者代码块时会进入这种状态。
<p>WAITING:等待状态,当线程调用Thread.sleep()方法、或者调用Object.wait()方法、或者有其他线程调用Thread.join方法、或者线程调用LockSupport.park()方法时会处于这种状态。
<p>TIMED_WAITING:和WAITING状态类似,但是会指定等待的时间。调用Thread.sleep(long)、Object.wait(long)、Thread.join(long)、LockSupport.parkNanos(long)、LockSupport.parkUntil(Object blocker, long deadline)的时候会进入这个状态
<p>TERMINATED:进程结束

三、引申--synchronized和ReentrantLock实现“阻塞”时的线程状态的区别

小伙伴们去看ReentrantLock的源码就知道,ReentrantLock是通过LockSupport.park实现阻塞的。所以synchronized实现阻塞后,等待的线程状态是BLOCKED状态,而通过ReentrantLock实现阻塞后,现成的状态是WAITING状态。看下面一个栗子:

class TestObject {
   public void testMethod(){
        synchronized (TestObject.class){
            try {
                Thread.sleep(1000000000000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class SynchronizedThread extends Thread{
    @Override
    public void run() {
        TestObject to = new TestObject();
        to.testMethod();
    }
}

public class Test {
    public static void main(String[] args) {
        SynchronizedThread st1 = new SynchronizedThread();
        SynchronizedThread st2 = new SynchronizedThread();
        st1.setName("1111111111");
        st2.setName("2222222222");
        st1.start();
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        st2.start();
    }
}

运行后通过jstack命令查看线程状态:

Paste_Image.png

可以看到线程命是‘2222222222’的线程状态是BLOCKED

修改一下代码:

class TestObject {
    ReentrantLock lock;
    public TestObject(ReentrantLock lock){
        this.lock = lock;
    }
    public void testMethod(){
        lock.lock();
        try {
            Thread.sleep(1000000000000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

class SynchronizedThread extends Thread{
    ReentrantLock lock;
    public SynchronizedThread(ReentrantLock lock){
        this.lock = lock;
    }
    @Override
    public void run() {
        TestObject to = new TestObject(lock);
        to.testMethod();
    }
}

public class Test {
    ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) {
        ReentrantLock lock = new ReentrantLock();
        SynchronizedThread st1 = new SynchronizedThread(lock);
        SynchronizedThread st2 = new SynchronizedThread(lock);
        st1.setName("1111111111");
        st2.setName("2222222222");
        st1.start();
        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        st2.start();
    }
}

查看线程状态:

Paste_Image.png

可以看到线程命是‘2222222222’的线程状态是WAITING

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 第三章 Java内存模型 3.1 Java内存模型的基础 通信在共享内存的模型里,通过写-读内存中的公共状态进行隐...
    泽毛阅读 4,514评论 2 21
  • 线程 线程是进程中的一个单一顺序的执行单元,也被称为轻量进程(lightweight process)。线程有自己...
    代码界的萧敬腾阅读 1,268评论 0 0
  • 线程 线程是进程中的一个单一顺序的执行单元,也被称为轻量进程(lightweight process)。线程有自己...
    代码界的扫地僧阅读 326评论 0 1
  • 下面是我自己收集整理的Java线程相关的面试题,可以用它来好好准备面试。 参考文档:-《Java核心技术 卷一》-...
    阿呆变Geek阅读 15,162评论 14 507
  • 今天1万字的任务是绝对完成不了了。 我现在虽然在锻炼,已经半个小时多了,才走了三公里。 跟我的期望是完全不相符的,...
    圈圈o0阅读 176评论 1 0

友情链接更多精彩内容