现在的位置: 首页 > 综合 > 正文

关于join方法的一个例子

2013年03月19日 ⁄ 综合 ⁄ 共 905字 ⁄ 字号 评论关闭

 

package test;

public class JoinTest {

 /**
  *
  * @author Administrator/2012-3-1/上午09:50:15
  */
 public static void main(String[] args) {

  int threadNumber = 10;  
        for (int i = 0; i < threadNumber; i++) {  
            final int threadID = i;   
            Thread thread = new Thread() {  
                public void run() {  
                    try {  
                        Thread.sleep((long) (Math.random() * 10000));  
                    } catch (InterruptedException e) {  
                        e.printStackTrace();  
                    }  
                    System.out.println(String.format("threadID:[%s] finished!!", threadID));  

                }  
            };  
              
            thread.start();  
            try {
    thread.join();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }  
        }  
          
        System.out.println("main thread finished!!");  
  
  
  

 }

}

 

 

注意: 通过join方法调用子线程是串行到主线程,不是并发

【上篇】
【下篇】

抱歉!评论已关闭.