【JUC并发】黑马程序员:线程的六种状态及状态转换

发布于:2023-02-06 ⋅ 阅读:(759) ⋅ 点赞:(0)

一、线程状态介绍

根据 Thread.State 枚举,jAVA种线程分为六种状态

  • NEW
  • RUNNABLE
  • BLOCK
  • WATING
  • TIMED_WAITING
  • TERMINATED
    在这里插入图片描述

注意:
操作系统层面线程分为五中状态:初始状态、可运行状态、运行状态、阻塞状态、终止状态

二、转换实例

2.1 NEW -> RUNNABLE : start()

@Test
    public void demo1() throws InterruptedException {
        Thread t1;
        t1=new Thread(()-> {
            log.debug(String.valueOf(Thread.currentThread().getState()));
        },"t1");
        log.debug(String.valueOf(t1.getState()));
        t1.start();
    }
14:49:21 [main] c.four-Demo5 - NEW
14:49:21 [t1] c.four-Demo5 - RUNNABLE

2.2 RUNNABLE -> WAITING :wait() park() join()

@Test
    public void demo2() throws InterruptedException {
        Thread t2=new Thread(()-> {
            log.debug(String.valueOf(Thread.currentThread().getState()));
            synchronized (lock){
                try {
                    lock.wait(); //wait() park() join()
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t2");
        t2.start();
        TimeUnit.SECONDS.sleep(2);
        log.debug(String.valueOf(t2.getState()));
    }
14:51:42 [t2] c.four-Demo5 - RUNNABLE
14:51:44 [main] c.four-Demo5 - WAITING

2.3 RUNNABLE -> TIME_WAITING :wait(n) parkNanos(n) join(n) sleep(n)

@Test
    public void demo3() throws InterruptedException {
        Thread t3=new Thread(()-> {
            log.debug(String.valueOf(Thread.currentThread().getState()));
            synchronized (lock){
                try {
                    lock.wait(5000); //wait(n) parkNanos(n) join(n) sleep(n)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t3");
        t3.start();
        TimeUnit.SECONDS.sleep(2);
        log.debug(String.valueOf(t3.getState()));
    }
14:52:41 [t3] c.four-Demo5 - RUNNABLE
14:52:43 [main] c.four-Demo5 - TIMED_WAITING

2.4 RUNNABLE -> BLOCK :synchronized lock

@Test
    public void demo4() throws InterruptedException {
        Thread t4=new Thread(()-> {
            synchronized (lock){
                try {
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"t4");
        t4.start();
        Thread t5=new Thread(()-> {
            log.debug(String.valueOf(Thread.currentThread().getState()));
            synchronized (lock){
            }
        },"t5");
        t5.start();
        TimeUnit.SECONDS.sleep(1);
        log.debug(String.valueOf(t5.getState()));
    }
14:53:19 [t5] c.four-Demo5 - RUNNABLE
14:53:20 [main] c.four-Demo5 - BLOCKED

2.5 RUNNABLE -> TERMINATED :run()执行完毕

@Test
    public void demo5() throws InterruptedException {
        Thread t1=new Thread(()-> {
            log.debug(String.valueOf(Thread.currentThread().getState()));
        },"t1");
        t1.start();
        TimeUnit.SECONDS.sleep(1);
        log.debug(String.valueOf(t1.getState()));
    }
14:53:54 [t1] c.four-Demo5 - RUNNABLE
14:53:55 [main] c.four-Demo5 - TERMINATED
本文含有隐藏内容,请 开通VIP 后查看