Java线程状态到底是5种还是6种?看这篇文章就够!

前言:网络上的文章杂乱无章,有的说线程是5种也有的说是6种状态,这里我们给出答案:6种

6种线程状态

java.lang.Thread类中明确声明了线程的6种状态,查看Thread的State枚举类,分别是NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED

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

  那线程的5种状态又是从何而来?答案是:5种状态是针对OS操作系统的线程状态,分别是NEW, RUNNABLE, RUNNING, BLOCKED, DEAD
  Java线程将操作系统线程状态的RUNNABLE, RUNNING, BLOCKED合并为一个状态:Runnable状态,具体如下图:
在这里插入图片描述
  现在对这些状态分别测试一下,创建6个线程,分别让其处于特定的状态

public class MyThread {
    public static void main(String[] args) throws InterruptedException {
        //NEW
        Thread t1 = new Thread(()->{

        }, "t1");

        //RUNNABLE
        Thread t2 = new Thread(()->{
            while (true) {
            }
        });
        t2.start();

        //WAITING
        Thread t3 = new Thread(()->{
            synchronized (MyThread.class){
                try {
                    t2.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t3.start();

        //TIMED_WAITING
        Thread t4 = new Thread(()->{
            try { Thread.sleep(1000000); } catch (InterruptedException e) { e.printStackTrace(); }
        });
        t4.start();

        //BLOCKED
        Thread t5 = new Thread(()->{

            synchronized (MyThread.class){

            }
        });
        t5.start();

        //TERMINATED
        Thread t6 = new Thread(()->{

        });
        t6.start();

        Thread.sleep(1000);
        System.out.println(t1.getState());
        System.out.println(t2.getState());
        System.out.println(t3.getState());
        System.out.println(t4.getState());
        System.out.println(t5.getState());
        System.out.println(t6.getState());

    }
}

  输出结果如下

NEW
RUNNABLE
WAITING
TIMED_WAITING
BLOCKED
TERMINATED

  至于各个状态是如何相互转换的,下篇文章再谈。

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sadness°

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值