Java未来。它在调试模式下成功但在我正常运行时失败

时间:2014-05-15 17:35:47

标签: java multithreading debugging future callable

伙计们,我面临着类似的情况 喜欢 This junit case on another thread 虽然我没有junit案件。我尝试了我所知道的一切......包括在该链接页面上的建议,保持倒计时和线程睡眠,但结果不会改变。如果我运行调试并给它一些时间它显示所有线程的所有结果,但如果我正常运行它总是给我较少的结果。 我的代码如下

`

    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;

    }
}

`

1 个答案:

答案 0 :(得分:0)

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