Java之多线程状态详解

线程状态

java.lang.Thread.State枚举类型中(内部类形式),定义了线程的几种状态,其代码结果为:

public class Thread {
    /* Java thread status for tools,
     * initialized to indicate thread 'not yet started'
     */
    private volatile int threadStatus = 0;
    
    public enum State {
        //Thread state for a thread which has not yet started.
        NEW,

        RUNNABLE,

        /**
         * Thread state for a thread blocked waiting for a monitor lock.
         * {@link Object#wait() Object.wait}.
         */
        BLOCKED,

        /**
         * Thread state for a waiting thread.
         * 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:
         */
        TIMED_WAITING,

        // Thread state for a terminated thread.
        TERMINATED;
    }
    
    //返回线程当前所处的状态
    public State getState() {
        // get current thread state
        return sun.misc.VM.toThreadState(threadStatus);
    }
}

状态解释:
在这里插入图片描述

线程状态变化的情况如下:
在这里插入图片描述

一个线程从创建到启动、到运行、到死亡,以及期间可能出现的情况都在上图中进行了描述。

注意:当前大家对线程状态有个大致印象即可,后续课程会详细讲解各种线程状态。

基础案例:

package com.briup.chap10;

public class Test13_State {
    public static void main(String[] args) throws InterruptedException {
		Thread th1 = new Thread("t1子线程") {
			@Override
			public void run() {
				for(int i = 0; i < 6; i++) {
					try {
						if(i == 3)
							Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					System.out.println("i: " + i);
				}
			}
		};
		
		//线程启动前,预计 NEW
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		
        //启动线程
		th1.start();
		
		// RUNNABLE  
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		System.out.println(th1.getState());

        //main线程sleep
		Thread.sleep(300);
            
		//此处:子线程依旧  sleep    TIMED_WAITING
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		System.out.println(th1.getState());
		
        //main线程sleep
		Thread.sleep(1000);
        
		//预计输出:终止状态
		System.out.println(th1.getState());
		System.out.println(th1.getState());
	}
}

//运行效果如下
NEW
NEW
RUNNABLE
RUNNABLE
RUNNABLE
RUNNABLE
RUNNABLE
RUNNABLE
i: 0
i: 1
i: 2
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
TIMED_WAITING
i: 3
i: 4
i: 5
TERMINATED
TERMINATED

案例结果分析:

  • 刚创建好的线程对象,就是出于NEW的状态

  • 线程启动后,会出于RUNNABLE状态,其包含俩种情况

    就绪状态,此时这个线程没有运行,因为没有抢到CPU的执行权

    运行状态,此时这个线程正在运行中,因为抢到CPU的执行权

  • JavaAPI中没有定义就绪状态和运行状态,而是统一叫做RUNNABLE(可运行状态),杰普课程中为了能更加清楚的描述问题,会用上就绪状态和运行状态

  • 线程多次抢到CPU执行权,"断断续续"把run方法执行完之后,就变成了TERMINATED状态(死亡)

    之所以"断断续续"的运行,是因为每次抢到CPU执行权的时候,只是运行很小的一个时间片,完了之后还要重新抢夺下一个时间片,并且中间还有可能抢不到的情况

状态转换图(对应上述案例):

在这里插入图片描述

从就绪状态到运行状态,之间会经过多次反复的CPU执行权的争夺(线程调度)

这就是一个线程经历的最基本的状态变化。

其他的状态都是线程在Running的时候,线程中调用了某些方法,或者触发了某些条件,导致这个线程进入到了阻塞状态(上面介绍的三种阻塞情况),后面陆续讲解。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值