多线程(四)Java线程的6个状态(用代码测试在控制台打印状态)

Java线程有多少个状态?
直接查看Thread.state类

public enum State {
    NEW,// 尚未启动的线程处于此状态
    RUNNABLE,// 在Java虚拟机中执行的线程处于此状态
    BLOCKED,//被堵塞等待监视器锁定的线程处于此状态
    WAITING,//等待,死死的等,等待另一个线程执行特定动作的线程处于此状态
    TIMED_WAITING,//超时等待,等待另一个线程执行动作达到指定等待时间的线程处于此状态。
    TERMINATED;//线程终止,已退出的线程处于此状态
}

/**
 * 测试 NEW RUNNABLE TIMED_WAITING TERMINATED
 * @author LCW
 * @since 2020/11/18 21:26
 **/
public class Test01 {
    public static void main(String[] args) throws InterruptedException {
        //启动的时候
        Thread thread = new Thread(() -> {
            //开始执行start(),线程显示RUNNABLE
            System.err.println(Thread.currentThread().getState());
            //执行sleep方法会使得线程进入TIMED_WAITING
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.err.println(Thread.currentThread().getState());
        }, "A");
        System.out.println("A==>" +thread.getState());//还没执行start方法,thread的状态就是NEW
        thread.start();//开始执行start(),线程显示RUNNABLE
        int i = 3;
        while (i-->0) {
            System.out.println("A==>" + thread.getState());
            TimeUnit.SECONDS.sleep(1);//线程结束TERMINATED
        }

    }
}

在这里插入图片描述


/**
 * 测试WAITING,该线程执行了wait()方法就会进入WAITING状态
 * @author LCW
 * @since 2020/11/18 21:26
 **/
public class Test02 {
    public static void main(String[] args) throws InterruptedException {
        //开启两个线程
        MyRunnable myRunnable = new MyRunnable();
        Thread threadA = new Thread(myRunnable, "A");
        Thread threadB = new Thread(myRunnable, "B");
        //线程A执行,进入WAITING状态
        threadA.start();
        int i = 3;
        while (i-- > 0) {//打印查看线程A的状态
            System.out.println("A==>" + threadA.getState());
            System.out.println("B==>" + threadB.getState());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        threadB.start();
        TimeUnit.SECONDS.sleep(1);
        System.out.println("A==>" + threadA.getState());
        System.out.println("B==>" + threadB.getState());

    }
}

class MyRunnable implements Runnable {

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("A")) {
            testA();
        } else {
            testB();
        }
    }

    private synchronized void testB() {//这个只是为了最后释放线程
        this.notifyAll();
    }

    public synchronized void testA() {//将该线程进程堵塞,此时线程A的State就是WAITING
        try {
            this.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

在这里插入图片描述


/**
 * 测试BLOCKED
 * @author LCW
 * @since 2020/11/18 22:40
 **/
public class Test03 {
    public static void main(String[] args) {
        MyRunnable2 myRunnable2 = new MyRunnable2();
        //当线程A,B需要获取同一把锁的时候
        Thread threadA = new Thread(myRunnable2, "A");
        Thread threadB = new Thread(myRunnable2, "B");
        threadA.start();
        threadB.start();
        int i = 3;
        while (i-- > 0) {//打印查看线程A的状态
            System.out.println("A==>" + threadA.getState());
            System.out.println("B==>" + threadB.getState());
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class MyRunnable2 implements Runnable {

    @Override
    public void run() {
      test();
    }

    private synchronized void test() {
        while (true) {

        }
    }

}

在这里插入图片描述

总结:

  1. NEW就是在线程还没有启动的时候的状态
  2. RUNNABLE 就是线程在执行start()方法后就进入的状态,下面的几种状态都是从RUNNABLE 变换过去的,也就是new第一,RUNNABLE 一定是第二个状态,其次才是下面的几种状态
  3. WAITING 就是当此线程执行了wait()方法的时候,线程进入的状态,死死的等待,直到其他线程执行了notify()或者notifyAll()方法才继续执行。
    注意:当线程执行wait()后,会释放锁,这跟sleep()不同,sleep()是不释放锁的。
  4. TIMED_WAITING 就是当线程执行了sleep()方法或者TimeUtil的sleep()方法,线程进入的状态
  5. BLOCKED就是当两个线程同时需要获取同一把锁,一个线程拿到了锁,那么另一个线程就进入了BLOCKED状态,等待拿到锁的线程执行完毕,才继续执行。
  6. TERMINATED就是线程结束后的状态
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值