閱讀37 返回首頁    go 阿裏雲 go 技術社區[雲棲]


關於join方法的一個例子

 

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方法調用子線程是串行到主線程,不是並發

最後更新:2017-04-02 22:16:29

  上一篇:go android 中使用socket使native和framework通信
  下一篇:go 編譯 android SLES/OpenSLESUT.h 找不到的解決辦法