Thread类中join()方法的使用(线程执行顺序,流程)

public class NewThread extends Thread { //NewThread是线程类
    public NewThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        int i = 100;
        while (i-- > 0) {
            try {
                sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(this.getName() + "---" + i);

        }
    }

    public static void main(String[] args) throws Exception {
        Thread son_1 = new NewThread("thread-1");//线程对象1: son_1
        Thread son_2 = new NewThread("thread-2");//线程对象2: son_2
        //son_1 和 son_2目前处于新建状态,不是就绪状态
        son_1.start(); //son_1处于就绪状态(具备抢占CPU的资格)
        son_2.start();//son_2处于就绪状态(具备抢占CPU的资格)

        //这个时候是 thread-1 和 thread-2 和 main 线程都处于就绪状态,都具有争夺CPU的权力,
        //然后当main线程接着往下执行,当执行到son_1.join()代码时,当前线程阻塞(也就是主线程阻塞)
        //此时具备抢占CPU线程的只有thread-1和thread-2了。
        //两线程交替执行线程内的run()方法。互不干扰
        //等着thread-1线程内的run()方法完全执行完毕后,
        //main线程复活
        //此时main线程和 thread-2线程都具备抢占CPU的权利。

        //情况一:
        //如果main线程抢占到了CPU了。
        //main线程输出一句 "main---活了"
        //然后main线程接着执行到son_2.join();
        //main线程阻塞。
        //此时只剩下thread-2线程,抢占CPU执行完run()方法内的剩余代码,
        //执行完之后,main线程复活。
        //main线程执行最后一句代码输出 all threads is over
        //main线程结束。
        //程序运行完毕!

        /**
         * 运行结果截图:
         * thread-1---0
         * main---活了
         * thread-2---0
         * all threads is over
         */


        //情况二:
        //如果thread-2抢占到了CPU。
        //thread-2线程执行完剩余的run()方法里面的代码
        //执行完成后,目前存活的线程只有main线程了
        //main线程输出一个 “main---活了”
        //main线程执行到son_2.join()方法,main线程阻塞
        //发现thread-2线程已经执行完毕,main线程复活
        //main线程接着又打印了一句“all threads is over”
        //全剧终!

        /**
         * 运行结果截图:
         * thread-1---1
         * thread-2---1
         * thread-1---0
         * thread-2---0
         * main---活了
         * all threads is over
         */

        //其实情况二还能再分(这种在该程序中不会出现,因为thread-2只要能抢占到CPU,基本上就会把剩余的run()中的代码执行完)
        //thread-2抢占到CPU,并且剩余run()方法中的代码没有执行完毕
        //thread-2进入就绪状态,main方法获取CPU的执行权,
        //main线程输出 “main---活了”
        //main线程遇到了son_2.join()代码
        //main线程阻塞
        //thread-2线程抢占CPU继续执行,执行完run()方法中的剩余代码!
        //main线程复活, 输出最后一句“all threads is over”
        //全剧终

        /**
         * 运行结果截图:
         * thread-1---0
         * thread-2---10
         * thread-2---9
         * thread-2---8
         * thread-2---7
         * main---活了
         * thread-2---6
         * thread-2---5
         * thread-2---4
         * thread-2---3
         * thread-2---2
         * thread-2---1
         * thread-2---0
         * all threads is over
         */



        son_1.join();//注意这里
        System.out.println(Thread.currentThread().getName() + "---活了");
        son_2.join();
        System.out.println("all threads is over");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值