2014-05-15 28 views
0

伙计们我面临类似的情况 像 This junit case on another thread 虽然我没有junit案件。我尝试了我所知道的一切......包括对该链接页面的建议,保持倒计时和线程睡眠,但结果不会改变。如果我通过调试运行,并给它一些时间,它显示了所有线程的所有结果,但如果我正常运行,它总会给我更少的结果。 我的代码初级讲座Java未来。它在调试模式下运行,但失败时,我通常运行

`

AtomicInteger atomicInteger = new AtomicInteger(employeeids.size()); 
    CountDownLatch latch = new CountDownLatch(employeeids.size()); 
    Iterable<List<String>> batchList = createBatches(employeeids, batchSize); 

    Set<Future<List<GradeSearchDTO>>> set = new HashSet<Future<List<GradeSearchDTO>>>(); 


    for(List<String> employeeidsList: batchList) { 

     Callable<List<GradeSearchDTO>> callable = new ScheduleCallable(employeetype, employeedetails, employeeidsList, dept, seeker, atomicInteger,latch); 
     Future<List<GradeSearchDTO>> future = pool.submit(callable); 
     set.add(future); 
     try { 
     Thread.sleep(1000); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 


    try { 
     latch.await(getTimeOutInMillis(), TimeUnit.MILLISECONDS); 
    } catch (InterruptedException e1) { 
     // TODO Auto-generated catch block 
     throw new EmployeeException("Building of Schedules didn't finish in time of ["+getTimeOutInMillis()+"] millis. "); 

    } 

    long timeLeft = getTimeOutInMillis(); 
    boolean check=true; 
    while (check){ 
     logger.debug("Waiting for building asset. countdown value is[" + timeLeft+"]"); 
     try { 
      Thread.sleep(TIME_TO_PAUSE); 
      timeLeft = timeLeft - TIME_TO_PAUSE; 
      if(timeLeft == 0 || timeLeft < 0){ 
       throw new EmployeeException("Building of Schedules didn't finish in time of ["+getTimeOutInMillis()+"] millis. "); 
      } 
      for (Future<List<GradeSearchDTO>> future : set) { 
       if(!future.isDone()){ 
        check=true; 
        break; 
       } 
       else{check=false;} 
       } 

     } catch (InterruptedException e) { 
      logger.error("Error waiting for asset to build to bulid"); 
     } 
    } 

    for (Future<List<GradeSearchDTO>> future : set) { 
     try { 
      EmployeeScheduleList.addAll(future.get()); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     } 



public static class ScheduleCallable implements Callable 

{ 
    private String employeetype; 
    private List<Employee> employeedetails; 
    private List<String> employeeidsList; 
    private String dept; 
    private EmployeeSeekerHelper seeker; 
    private AtomicInteger atomicInteger; 
    private CountDownLatch latch; 

    public ScheduleCallable(String employeetype,List<Employee> employeedetails, 
    list<String> employeeidsList, String dept,EmployeeSeekerHelper seeker,AtomicInteger 
    atomicInteger,CountDownLatch latch) 

    { 

    this.employeetype = employeetype; 
    this.employeedetails = employeedetails; 
    this.employeeidsList = employeeidsList; 
    this.dept = dept; 
    this.seeker = seeker; 
    this.atomicInteger=atomicInteger; 
    this.latch=latch; 
    } 

    public List<GradeSearchDTO> call() 
    { 
    List<GradeSearchDTO> EmployeeScheduleList = new ArrayList<GradeSearchDTO>(0) ; 

    int counter=1; 

    for(String scheduleId : employeeidsList) 

    { 

    latch.countDown(); 

    EmployeeScheduleList.addAll(searchEmployeeRulesForSchedule(employeetype,employeedetails,scheduleId,dept,seeker,latch)); 
    System.out.println("Thread COUNTER "+counter); 
    atomicInteger.decrementAndGet(); 
    counter++; 
    // latch.countDown(); 

    } 
return EmployeeScheduleList; 

    } 
} 

`

+4

TL; DR能否指出你遇到的问题并创建一小段代码来再现它? – maasg

+0

+1,如果您指定_what_失败,它也会有所帮助。你是否得到一个例外,如果是,那是什么?你是否在某个地方得到意想不到的结果,如果是的话,他们是什么,你期望什么? – yshavit

+0

假设我的batchList size = 5,每个对象包含10个employeeid。所以批处理列表循环应该运行5次,我会除了结果是从这5个线程..但我看到的是我有时从1线程有时2线程有时结果。我的线路试图工作的方式是在每个批处理列表对象(employeeid)的call()方法中,运行另一个循环来调用实际执行业务逻辑的方法。 – user3641298

回答

0

所以上面的代码是完全没有问题......没有错的。我遇到随机结果的问题是因为在调用()下执行业务逻辑的方法searchEmployeeRulesForSchedule(employeetype,employeedetails,scheduleId,dept,seeker,latch) 在内部调用规则引擎,该规则引擎由于使用相同的类实例而不是返回正确的结果每个线程的新实例。

相关问题