首页 > 代码库 > thread.join函数,java多线程中的join函数解析

thread.join函数,java多线程中的join函数解析

join函数的作用,是让当前线程等待,直到调用join()的 线程结束或者等到一段时间,我们来看以下代码

 1 package mian; 2  3  4 public class simpleplela { 5     static void threadMessage(String message) { 6         String threadName =  7                 Thread.currentThread().getName(); 8          9           System.out.println(threadName+"   "+message10                    );11     }12     13     private static class MessageLoop14         implements Runnable {15         public void run(){16             17         18         String importantInfo[] = {19                 "ONe",20                 "TWo",21                 "THREe",22                 "four"23             };24         try{25             for(int i = 0;26                     i<importantInfo.length;27                     i++){28                 Thread.sleep(4000);29                 threadMessage(importantInfo[i]);30             }31         }32         catch(InterruptedException e){33             threadMessage("I was‘nt done!");34         }35     }36     }37     public static void main(String args[])38         throws InterruptedException {39         long patience = 1000*10;40         if(args.length>0){41             try{42                 patience = Long.parseLong(args[0])*1000;43             }catch(NumberFormatException e){44                 System.err.println("Argument must be an integer");45                 System.exit(1);46             }47         }48         threadMessage("Starting MessageLoop Thread");49         long startTime = System.currentTimeMillis();50         Thread t = new Thread(new MessageLoop());51         t.start();52         threadMessage("Waiting for MEssageLoop Thread to finish");53         while((t.isAlive())){54             threadMessage("still waiting...");55         56             t.join();57             if (((System.currentTimeMillis() - startTime) > patience)58                     && t.isAlive()) {59                 threadMessage("Tired of waiting!");60                 t.interrupt();61                 // Shouldn‘t be long now62                 // -- wait indefinitely63                 t.join();64             }65             }66         threadMessage("Finally!");67     }68 69 }
View Code

输出如下

 1 package mian; 2  3  4 public class simpleplela { 5     static void threadMessage(String message) { 6         String threadName =  7                 Thread.currentThread().getName(); 8          9           System.out.println(threadName+"   "+message10                    );11     }12     13     private static class MessageLoop14         implements Runnable {15         public void run(){16             17         18         String importantInfo[] = {19                 "ONe",20                 "TWo",21                 "THREe",22                 "four"23             };24         try{25             for(int i = 0;26                     i<importantInfo.length;27                     i++){28                 Thread.sleep(4000);29                 threadMessage(importantInfo[i]);30             }31         }32         catch(InterruptedException e){33             threadMessage("I was‘nt done!");34         }35     }36     }37     public static void main(String args[])38         throws InterruptedException {39         long patience = 1000*10;40         if(args.length>0){41             try{42                 patience = Long.parseLong(args[0])*1000;43             }catch(NumberFormatException e){44                 System.err.println("Argument must be an integer");45                 System.exit(1);46             }47         }48         threadMessage("Starting MessageLoop Thread");49         long startTime = System.currentTimeMillis();50         Thread t = new Thread(new MessageLoop());51         t.start();52         threadMessage("Waiting for MEssageLoop Thread to finish");53         while((t.isAlive())){54             threadMessage("still waiting...");55         56             t.join(1000);57             if (((System.currentTimeMillis() - startTime) > patience)58                     && t.isAlive()) {59                 threadMessage("Tired of waiting!");60                 t.interrupt();61                 // Shouldn‘t be long now62                 // -- wait indefinitely63                 t.join();64             }65             }66         threadMessage("Finally!");67     }68 69 }
View Code

输出如下 我们分析一下,两段代码只有一行不一样。第一段是t.join(),会让当前线程(例子中为主线程)一直等待,知道t结束;

第二段是t.join(1000),会让当前线程等待1000毫秒,之后继续。