solitaryclown

Java线程的六种状态

2021-12-18
solitaryclown

1. java线程-六种状态

1.1. 测试类

package com.huangbei.test;


import lombok.extern.slf4j.Slf4j;


/*
线程的六种状态:
    NEW,RUNNABLE,TERMINATED,TIMED_WAITING,WATING,BLOCKED
 */
@Slf4j(topic = "c.Test11")
public class Test11 {
    public static void main(String[] args) {
        //线程1,NEW
        Thread t1 = new Thread(() -> {
            ;
        }, "t1");
//        t1.start();

        //线程2,RUNNABLE
        Thread t2 = new Thread(() -> {
            while (true) {
                ;
            }
        }, "t2");
        t2.start();

        //线程3,TERMINATED
        Thread t3 = new Thread(() -> {
            ;
        }, "t3");
        t3.start();

        //线程4,TIMED_WAITING
        Thread t4 = new Thread(() -> {

            synchronized (Test11.class){
                try {
                    Thread.sleep(1000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }, "t4");

        t4.start();

        //线程5,WAITING
        Thread t5 = new Thread(() -> {
            try {
                t2.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t5");

        t5.start();

        //线程6,BLOCKED
        Thread t6 = new Thread(() -> {
            synchronized (Test11.class){
                ;
            }
        }, "t6");
        t6.start();




        log.debug("State of Thread[t1]:{}",t1.getState());
        log.debug("State of Thread[t2]:{}",t2.getState());
        log.debug("State of Thread[t3]:{}",t3.getState());
        log.debug("State of Thread[t4]:{}",t4.getState());
        log.debug("State of Thread[t5]:{}",t5.getState());
        log.debug("State of Thread[t6]:{}",t6.getState());
    }
}

1.2. 测试结果

14:26:45.795 [main] c.Test11 - State of Thread[t1]:NEW
14:26:45.795 [main] c.Test11 - State of Thread[t2]:RUNNABLE
14:26:45.795 [main] c.Test11 - State of Thread[t3]:TERMINATED
14:26:45.795 [main] c.Test11 - State of Thread[t4]:TIMED_WAITING
14:26:45.795 [main] c.Test11 - State of Thread[t5]:WAITING
14:26:45.795 [main] c.Test11 - State of Thread[t6]:BLOCKED

Comments

Content