线程的状态|生命周期(通过代码案例简单而深入的理解线程的状态)

8 篇文章 0 订阅
2 篇文章 0 订阅

线程的生命周期/状态state

线程状态变化流程图

在这里插入图片描述

线程状态

NEW
案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 11:48
 */
public class NewStatus {

    public static void main(String[] args) {
        Thread thread=new Thread("thread-a");
        System.out.println("当前线程状态 = "+thread.getState());
    }
}
控台输出
当前线程状态 = NEW

Process finished with exit code 0

RUNNABLE和RUNNING
案例举例

创建一个带有线程处理逻辑的线程类StatusThreadDemo

package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-10 16:37
 */
public class StartStatus {

    public static void main(String[] args) {
        StatusThreadDemo thread=new StatusThreadDemo();
        System.out.println("start前线程状态 = "+thread.getState());
        thread.start();
        System.out.println("start后线程状态 = "+thread.getState());
        try {
            thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("start后线程正在运行的状态 = "+thread.getState());


    }
}

class StatusThreadDemo extends Thread{
    @Override
    public void run() {
        for (int i=0;i<5;i++){
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("StatusThreadDemo i="+i);
        }
    }
}


控台输出
start前线程状态 = NEW
start后线程状态 = RUNNABLE
StatusThreadDemo i=0
StatusThreadDemo i=1
StatusThreadDemo i=2
StatusThreadDemo i=3
StatusThreadDemo i=4

Process finished with exit code 0
备注说明
1.这里注意下,start()方法调用成功之后,并不是立刻进行运行,而是状态由New转为Runnable状态
  而后,由操作系统进行调度转为Running运行状态;
2.这里大家需要注意下:Running状态如果由于某种原因被阻塞(如sleep中)或者由于某些原因,指令无法运行,
  那么状态也会转为Runnable,等待操作系统重新调度;
3.这里的操作系统的切换,实际上就是CPU在线程中的调度切换;
4.我们上面演示的“start后线程状态 =”不一定是RUNNABLE 也有可能刚好碰上代码在sleep 就是Timed Wating
TIMED_WAITING
主要是你在调用下面方法的时候,待了超时时间
join
sleep
wait
LockSupport.parkUnit
案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 15:25
 */
public class TimedWaitingStatus {

    public static void main(String[] args)  {
        Thread thread=new Thread(new TimedWaitingThreadDemo());
        thread.start();
        System.out.println("线程sleep中的当前状态 = "+thread.getState());
    }
}



class TimedWaitingThreadDemo implements Runnable{

    @Override
    public void run() {
        System.out.println("开始sleep(5s)方法的调用");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束sleep(5s)方法的调用");

    }
}
控台输出
Connected to the target VM, address: '127.0.0.1:65084', transport: 'socket'
开始sleep(5s)方法的调用
线程sleep中的当前状态 = TIMED_WAITING
结束sleep(5s)方法的调用
Disconnected from the target VM, address: '127.0.0.1:65084', transport: 'socket'

Process finished with exit code 0
备注说明
WAITING
join
wait
LockSupport.parkUnit
1.上面的方法调用的时候,不带超时时间
案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 15:36
 */
public class WaitingStatus {
    public static void main(String[] args) throws InterruptedException {

        Thread thread1=new Thread(new WaitingThreadDemo(),"thread1");
        thread1.start();
        System.out.println(thread1.getState());
    }
}

class WaitingThreadDemo implements Runnable{
    
    @Override
    public void run() {
        try {
            Thread.currentThread().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Hello 大家好,我是WaitingThreadDemo ="+Thread.currentThread().getName());
    }
}
控台输出
WAITING

Process finished with exit code 130 (interrupted by signal 2: SIGINT)
备注说明
Blocked
1.Blocked阻塞状态是一种锁阻塞的状态;
  如 synchronized的锁
案例举例
package com.gaoxinfu.demo.jdk.rt.java.lang.thread.status;

/**
 * @Description:
 * @Author: gaoxinfu
 * @Date: 2020-06-11 16:04
 */
public class BlockedStatus {


    public static void main(String[] args) {

        Thread thread1=new Thread(new BlockedThreadDemo(),"thread1");
        Thread thread2=new Thread(new BlockedThreadDemo(),"thread1");
        thread1.start();
        thread2.start();
        System.out.println("thread1线程状态 = " +thread1.getState());
        System.out.println("thread2线程状态 = " +thread2.getState());

    }
}

class BlockedThreadDemo implements Runnable{

    @Override
    public void run() {
        synchronized (this){
            try {
                System.out.println("当前线程名字 = "+Thread.currentThread().getName()+",开始锁定代码执行片段");
                Thread.currentThread().sleep(5000);
                System.out.println("当前线程名字 = "+Thread.currentThread().getName()+",结束锁定代码执行片段");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
控台说明
当前线程名字 = thread1,开始锁定代码执行片段
thread1线程状态 = TIMED_WAITING
thread2线程状态 = BLOCKED
当前线程名字 = thread1,开始锁定代码执行片段
当前线程名字 = thread1,结束锁定代码执行片段
当前线程名字 = thread1,结束锁定代码执行片段

Process finished with exit code 0
TERMINATED
1.线程终止状态,也就是线程任务结束时的一个状态
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

东山富哥

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

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

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

打赏作者

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

抵扣说明:

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

余额充值