阅读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 找不到的解决办法