Example #1
0
 /**
  * 汇集任务结果
  *
  * @param <E>
  * @return E类型的集合
  */
 public <E> List<E> join() {
   synchronized (lock) {
     while (lock.thread_count > 0) { // 检查线程数,如果为0,则表示所有任务处理完成
       // System.out.println("threadCount: "+THREAD_COUNT);
       try {
         // 如果任务没有全部完成,则挂起。等待完成的任务给予通知
         lock.wait();
       } catch (InterruptedException e) {
         e.printStackTrace();
       }
     }
   }
   List list = new ArrayList();
   // 取出每个任务的处理结果,汇总后返回
   for (Future future : futres) {
     try {
       // 因为任务都已经完成,这里直接get
       Object result = future.get();
       if (result != null) {
         if (result instanceof Collection) list.addAll((Collection) result);
         else list.add(result);
       }
     } catch (Exception e) {
       e.printStackTrace();
     }
   }
   return list;
 }