04.join与interrupt

join加入
public class JoinDemo {
    public volatile static int i = 0;
    public static class AddThread extends Thread{
        @Override
        public void run() {
            for (i=0;i<10000000;i++);
        }
    }
    public static void main(String[] args) throws InterruptedException{
        AddThread addThread = new AddThread();
        addThread.start();
        addThread.join();
        System.out.println(i);
    }
}
public class JoinDemo {
    public static void main(String[] args) throws InterruptedException{
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("child threadA over");
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("child threadB over");
            }
        });
        threadA.start();
        threadB.start();
        System.out.println("wait all thread over");
        //等待子线程执行完毕
        threadA.join();//主线程调用threadA的join()方法后被阻塞,等待threadA执行完毕
        threadB.join();
        System.out.println("all thread over");
        //wait all thread over
        //child threadA over
        //child threadB over
        //all thread over

        Thread threadOne = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("threadOne begin run");
                while (true){} //死循环
            }
        });
        //获取主线程
        final Thread mainThread = Thread.currentThread();
        Thread threadTwo = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //中断主线程
                mainThread.interrupt();
                System.out.println("threadTwo begin run");
            }
        });
        threadOne.start();
        threadTwo.start();
        threadOne.join();
        //threadOne begin run
        //threadTwo begin run
        //Exception in thread "main" java.lang.InterruptedException

        //在 threadOne 线程里面执行死循环,主线程调用 threadOne 的 join 方 法阻 塞 自己 等 待线程 threadOne 执行完毕,
        // 待 threadTwo 休眠 ls 后会调用主线程的 interrupt() 方法设置主线程的中断标志,
        // 从结果看在主线程中的 threadOne.join() 处会抛 出 InterruptedException 异常
    }
}

转载于:https://www.cnblogs.com/fly-book/p/11361670.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值