Java中线程的状态

Java中线程的状态

在Java当中把对线程的状态分得比原生操作系统更加细致,在Thread类内部,定义了一个枚举类型State,定义了线程的6种状态,如下

public enum State {
        
        NEW,

        RUNNABLE,

        BLOCKED,

        WAITING,

        TIMED_WAITING,

        TERMINATED;
    }
  1. NEW:线程刚创建出来,还未执行start()方法之前处于NEW状态.
 	    Thread thread = new Thread(new ThreadMethod());
        System.out.println("thread.getState() = " + thread.getState()); 

在这里插入图片描述

2.RUNNABLE:线程处于运行状态或者处在就绪队列,等待CPU资源

	   Thread thread = new Thread(new ThreadMethod());
       System.out.println("thread.getState() = " + thread.getState());
       thread.start();
       System.out.println("thread.getState() = " + thread.getState());

在这里插入图片描述

3.TERMINATED结束状态,表示线程已经执行完毕

       Thread thread = new Thread(new ThreadMethod());
       System.out.println("thread.getState() = " + thread.getState());
       thread.start();
       System.out.println("thread.getState() = " + thread.getState());
       Thread.sleep(100);
       System.out.println("thread.getState() = " + thread.getState());

在这里插入图片描述

4.BLOCKED: 这个状态表示线程正在等待锁资源

public class ThreadDemo implements Runnable {

    private static final Object lock = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new ThreadDemo());
        thread.start();
        Thread thread1 = new Thread(new ThreadDemo());
        thread1.start();
        Thread.sleep(1000);
        System.out.println("thread1.getState() = " + thread1.getState());
    }
    @Override
    public void run() {
        synchronized (lock){
            System.out.println(Thread.currentThread().getName()+" synchronized 加锁");
            // 休眠 5 秒
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+" synchronized 释放锁");
        }
    }
}

可以看到,当thread开始执行时,进入synchronized 代码块,而后来的线程thread因为无法进入synchronized而阻塞,进入BLOCKED状态
在这里插入图片描述

5.WAITING: 查看源码–>
在这里插入图片描述
发现,当线程调用上面3个方法时,会进入WAITING状态
如:

public class ThreadState implements Runnable{
    @Override
    public synchronized void run() {
        while (true) {
            try {
                this.wait();
                this.notify();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new ThreadState());
        thread.start();
        Thread.sleep(1000);
        System.out.println("thread.getState() = " + thread.getState());
    } }

在这里插入图片描述

6.TIMED_WAITING: 查看源码–>
在这里插入图片描述
当线程进入有限等待时进入TIMED_WAITING状态.

如:

public class ThreadState implements Runnable{
    @Override
    public void run() {
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new ThreadState());
        thread.start();
        Thread.sleep(1000);
        System.out.println("thread.getState() = " + thread.getState());
    } }

输出
在这里插入图片描述
同理join()与wait()带参数的版本可以达到同样的效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力写代码的菜鸟

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

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

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

打赏作者

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

抵扣说明:

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

余额充值