JAVASE_线程生命周期实现

目录

1.含义

2.线程周期的六种状态(枚举State)

3.执行过程——代码展示

4.图片详解


1.含义

        线程从创建到终止的过程

2.线程周期的六种状态(枚举State)

        NEW新建状态       

        RUNNABLE运行状态

        BLOCK阻塞状态

        TIMED_WAITING有限时等待状态

        WAITING无限时等待状态

        TERMINATED终止状态

3.执行过程——代码展示

       ① 当开始使用new实例化Thread对象的时候——NEW新建状态

        代码展示:

public static void main(String[] args) {
        final Test8 test8 = new Test8();
        /*新建状态*/
        Thread t1 = new Thread(){
            @Override
            public void run() {
                test8.test();
            }
        };
            //获取状态
        System.out.println("新建状态" + t1.getState());
}

        结果:

新建状态NEW

       ② 对象调用.start()方法——RUNNABLE运行状态

         代码展示

public static void main(String[] args) {
        final Test8 test8 = new Test8();
        /*新建状态*/
        Thread t1 = new Thread(){
            @Override
            public void run() {
                test8.test();
            }
        };
        t1.start()
            //获取状态
        System.out.println("运行状态" + t1.getState());
}

        结果:

运行状态RUNNABLE

       ③ 当线程处于RUNNABLE状态,如果竞争锁,但是没有持有锁,就会切换到BLOCK状态,如果竞争到锁,执行完成后,再切换到RUNNBALE运行状态;

        代码展示:

package com.bjsxt.test;

public class Test11 {
    public synchronized  void test(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final Test11 test11 = new Test11();
        //第一个子线程
        Thread t1 = new Thread(){
            @Override
            public void run() {
                test11.test();
            }
        };
        t1.start();
        //第二个子线程
        Thread t2 = new Thread(){
            @Override
            public void run() {
                test11.test();
            }
        };

        t2.start();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //获取状态
        System.out.println("BLOCK状态" + t2.getState());
    }
}

        结果:

BLOCK状态BLOCKED

        ④当线程处于RUNNABLE状态,对象调用.sleep()方法,就会切换到TIMED_WAITING有限时等待状态,等到休眠结束,切换到RUNNBALE运行状态;

 代码展示;

public class Test8 {

    public synchronized  void test(){
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    }

    public static void main(String[] args) {
        final Test8 test8 = new Test8();
        Thread t1 = new Thread(){
            @Override
            public void run() {
                test8.test();
            }
        };          
        t1.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("有限时等待状态" + t1.getState());
    }
}

         结果:

有限时等待状态TIMED_WAITING

        ⑤当线程处于RUNNABLE状态,对象调用.wait()方法,就会切换到WAITING无限时等待状态,需要调用notify()或notifyAll()方法后,切换到RUNNBALE运行状态;

 代码展示;

package com.bjsxt.test;

public class Test6 {

    public static void main(String[] args) {
        final Class<Test6> tc = Test6.class;
        Thread t1 = new Thread(){
            @Override
            public void run() {
                synchronized (tc){
                    try {
                        tc.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t1.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("状态:\t" + t1.getState());

        Thread t2 = new Thread(){
            @Override
            public void run() {
                synchronized (tc){
                        tc.notify();
                }
            }
        };
        t2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("使用notify()恢复t1后:\t" + t1.getState());
    }
}

结果:

状态:	WAITING
使用notify()恢复t1后:	TERMINATED

       ⑥ 进入TERMINATED终止状态条件:线程执行完成 / Error / Exception;

 代码展示:

package com.bjsxt.test;

public class Test11 {
    public synchronized  void test(){
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        final Test11 test11 = new Test11();
        /*新建状态*/
        Thread t1 = new Thread(){
            @Override
            public void run() {
                test11.test();
            }
        };
        t1.start();
        t1.stop();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        //获取状态
        System.out.println("TERMINATED状态" + t1.getState());
    }
}

        结果: 

TERMINATED状态TERMINATED

4.图片详解

        

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值