join()方法作用

join()方法作用

当在主线程当中执行到t1.join()方法时,就认为主线程应该把执行权让给t1

废话不多说看代码:

package com.toov5.thread;

public class JoinThreadTest {

       public static void main(String[] args) {
        
        Thread t1 = new Thread(new Runnable() {
                
                @Override
                public void run() {
                    for(int i=0;i<10;i++){
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                        System.out.println(Thread.currentThread().getName()+"i"+i);
                    }
                    
                }
            });
        t1.start();
        
        //主线程在此调用t1.join()方法,就认为主线程应该把执行权交给t1 让t1执行完毕后再执行主线程
        try {
            t1.join();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
//        t1.start();
        
        for(int i=0; i<10;i++){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             System.out.println("main"+"i"+i); 
        }
       
    }
    
    
}

如果先调用join的方法在执行 启动线程

package com.toov5.thread;

public class JoinThreadTest {

       public static void main(String[] args) {
        
        Thread t1 = new Thread(new Runnable() {
                
                @Override
                public void run() {
                    for(int i=0;i<10;i++){
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            // TODO: handle exception
                        }
                        System.out.println(Thread.currentThread().getName()+"i"+i);
                    }
                    
                }
            });
//        t1.start();
        
        //主线程在此调用t1.join()方法,就认为主线程应该把执行权交给t1 让t1执行完毕后再执行主线程
        try {
            t1.join();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        t1.start();
        
        for(int i=0; i<10;i++){
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             System.out.println("main"+"i"+i); 
        }
       
    }
    
    
}

结果分别:

 

其实质就是类似于一个加入线程

 join(), 当前线程暂停, 等待指定的线程执行结束后, 当前线程再继续
 join(int), 可以等待指定的毫秒之后继续

package com.toov5.test;

public class demoJoin {

    public static void main(String[] args) {
    Thread t1= new Thread() {
            @Override
            public void run() {
                for(int i =0; i<10; i++) {
                    System.out.println(getName()+"...bbbbb");
                }
                 
            }
            
        };
        
    Thread t2 = new Thread() {
        public void run() {
            for (int i = 0; i < 10; i++) {
                if (i==2) {
                    try {
                        t1.join();
                    } catch (InterruptedException e) {
                        
                        e.printStackTrace();
                    }
                }
                System.out.println(getName()+"...aaaaa");
                
            }
            
        };
    };    
        
       t1.start();
       t2.start();
    
    }
    
     
}

可以看到:

aaa执行了两次后 就开始执行 bbb一直执行完毕 才可以执行 aaaaaa

 也可以执行插队时间 join(1)    //过了指定的插队时间后 两个线程交替执行

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值