线程的六种状态

发布时间 2023-12-02 16:12:56作者: 林浅

New:初始状态 (未调用start )刚刚创建完

Runnable:运行状态 执行start

Blocked:阻塞状态  

Waiting:等待状态 通过Join sleep等方法使线程在等待

Timed Waiting :计时等待状态   sleep join 等方法设置了参数

Terminated:终止状态,线程执行完毕

package org.example.test1;

public class StateThread {
    public static void test1(){//NEW
        Thread t1 = new Thread(()->{
            System.out.println("Thread1");
        });
        System.out.println(t1.getState());
    }
    public static void test2() throws InterruptedException {//Terminated
        Thread t1 = new Thread(()->{
            System.out.println("线程开始执行");
            System.out.println("线程结束执行");
        });
        t1.start();
        Thread.sleep(3000);
        System.out.println(t1.getState());
    }
    public static void test3(){//Runnable
        Thread t1 = new Thread(()->{

        });
        t1.start();
        System.out.println(t1.getState());
    }
    public static void test4() throws InterruptedException {//Blocked
        class Table{
            public synchronized void use(){
                System.out.println(Thread.currentThread().getName()+"-使用桌子");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+"-使用完毕");
            }
        }
        System.out.println(Thread.currentThread().getName());
        Table table = new Table();
        Thread student1 = new Thread(()->{
            table.use();
        },"s1");
        Thread student2 = new Thread(()->{
            table.use();
        },"s2");
        student1.start();
        Thread.sleep(1000);
        student2.start();
        Thread.sleep(1000);
        System.out.println(student2.getState());
    }
    public static void test5() throws InterruptedException {//Waiting
        class Table{
            public synchronized void use() throws InterruptedException {
                System.out.println(Thread.currentThread().getName()+"-使用桌子");
                System.out.println("忘记点餐了");
                wait();
                System.out.println(Thread.currentThread().getName()+"-使用完毕");
            }
        }
        Table table = new Table();
        Thread student1 = new Thread(()->{
            try {
                table.use();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"s1");
        student1.start();
        Thread.sleep(100);
        System.out.println(student1.getState());
    }
    public static void test6() throws InterruptedException {//Timed_Waiting
        class Table{
            public synchronized void use() throws InterruptedException {
                System.out.println(Thread.currentThread().getName()+"-使用桌子");
                System.out.println("忘记点餐了");
                wait(3000);
                System.out.println(Thread.currentThread().getName()+"-使用完毕");
            }
        }
        Table table = new Table();
        Thread student1 = new Thread(()->{
            try {
                table.use();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"s1");
        student1.start();
        Thread.sleep(100);
        System.out.println(student1.getState());
    }

    public static void main(String[] args) throws InterruptedException {
        //test1();
        //test2();
        //test3();
        //test4();
        //test5();
        test6();
    }
}