zoukankan      html  css  js  c++  java
  • 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)    //过了指定的插队时间后 两个线程交替执行

    注意: 要在start之后 调用join 否则不起作用 

  • 相关阅读:
    向iphone模拟器中导入图片
    键盘样式风格设置及隐藏
    (转)iPhone图片处理:摄像头/相册获取图片,压缩图片,上传服务器,下载,拉伸,方法总结
    python通过get方式,post方式发送http请求和接收http响应-urllib urllib2
    Python中 如何使用telnet 检测端口是否通
    Linux运维人员共用root帐户权限审计
    python ssh登录
    欢迎来到 Flask 的世界
    大杂烩
    随笔
  • 原文地址:https://www.cnblogs.com/toov5/p/9826492.html
Copyright © 2011-2022 走看看