JAVA多线程join方法入门详解

package com.yys.thread;

/**
 * Created by yys on 2017/9/20.
 * 测试 Thread join方法
 * join 方法为合并线程
 * 如果不加入join方法便是两个独立线程 (main线程,和MyThread2线程)
 * join 方法将两个线程合并 此处将MyThread2合并到main线程中 合并后t1.start()执行完成后执行main()方法中for循环
 * 输出为
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am abcde
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 i am main thread
 *
 */
public class TestJoin {
    public static void main(String args[]){
        MyThread2 t1 = new MyThread2("abcde");
        t1.start();

        try {
            t1.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for(int i=0;i<=10;i++){
            System.out.println("i am main thread");
        }
    }
}
class MyThread2 extends Thread{
    MyThread2(String name){
        super(name);
    }
    public void run(){
        for(int i=0;i<=10;i++){
            System.out.println("i am " + getName());
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值