wait until all threads finish their work in java

后端 未结 16 2191
情深已故
情深已故 2020-11-22 14:10

I\'m writing an application that has 5 threads that get some information from web simultaneously and fill 5 different fields in a buffer class.
I need to validate buffer

16条回答
  •  借酒劲吻你
    2020-11-22 14:46

    try this, will work.

      Thread[] threads = new Thread[10];
    
      List allThreads = new ArrayList();
    
      for(Thread thread : threads){
    
            if(null != thread){
    
                  if(thread.isAlive()){
    
                        allThreads.add(thread);
    
                  }
    
            }
    
      }
    
      while(!allThreads.isEmpty()){
    
            Iterator ite = allThreads.iterator();
    
            while(ite.hasNext()){
    
                  Thread thread = ite.next();
    
                  if(!thread.isAlive()){
    
                       ite.remove();
                  }
    
            }
    
       }
    

提交回复
热议问题