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 否則不起作用 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM