Java中线程的状态

操作系统上

  • 初始状态】创建了线程对象,还未与操作系统线程关联

  • 可运行状态】线程已经被创建,可以由 CPU 调度执行

  • 运行状态】获取了 CPU 时间片运行中的状态

    • 当 CPU 时间片用完,会从【运行状态】转换至【可运行状态】,会导致线程的上下文切换

  • 阻塞状态

    • 如果调用了阻塞 API,如 BIO 读写文件,这时该线程实际不会用到 CPU,会导致线程上下文切换,进入 【阻塞状态】

    • 等 BIO 操作完毕,会由操作系统唤醒阻塞的线程,转换至【可运行状态】

    • 与【可运行状态】的区别是,对【阻塞状态】的线程来说只要它们一直不唤醒,调度器就一直不会考虑调度它们

  • 终止状态】表示线程已经执行完毕,生命周期已经结束,不会再转换为其它状态

java层面上

 

  • 初始(NEW):新创建了一个线程对象,但还没有调用start()方法。

  • 运行(RUNNABLE):Java线程中将就绪(ready)和运行中(running)两种状态笼统的称为“运行”。 线程对象创建后,其他线程(比如main线程)调用了该对象的start()方法。该状态的线程位于可运行线程池中,等待被线程调度选中,获取CPU的使用权,此时处于就绪状态(ready)。就绪状态的线程在获得CPU时间片后变为运行中状态(running)。

  • 阻塞(BLOCKED):表示线程阻塞于锁。

  • 等待(WAITING):进入该状态的线程需要等待其他线程做出一些特定动作(通知或中断)。

  • 超时等待(TIMED_WAITING):该状态不同于WAITING,它可以在指定的时间后自行返回

  • 终止(TERMINATED):表示该线程已经执行完毕。

java代码

public class ThreadStateTest {

    public static void main(String[] args) throws InterruptedException {
        //new
        Thread t1= new Thread(()->{
            System.out.println("t1");
        });

        //timed_waiting
        Thread t2= new Thread(()->{
            synchronized (ThreadStateTest.class){
                try {
                    Thread.sleep(100000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("t1");
        });
        t2.start();

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


        //runnable
        Thread t4= new Thread(()->{
            while(true){

            }

        });
        t4.start();

        //blocked
        Thread t5= new Thread(()->{
              synchronized (ThreadStateTest.class){

              }

        });
        t5.start();


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


        });
        t6.start();
        Thread.sleep(500);
        System.out.println("t1:"+ t1.getState());
        System.out.println("t2:"+ t2.getState());
        System.out.println("t3:"+ t3.getState());
        System.out.println("t4:"+ t4.getState());
        System.out.println("t5:"+ t5.getState());
        System.out.println("t6:"+ t6.getState());

    }
}

输出

t1:NEW
t2:TIMED_WAITING
t3:WAITING
t4:RUNNABLE
t5:BLOCKED
t6:TERMINATED

线程状态枚举类 java.lang.Thread.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;
    }

参考 

https://www.bilibili.com/video/BV16J411h7Rd/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值