[英]Java Future. It suceeds in debug mode but fails when i run it normally
伙计们,尽管我没有junit案例,但我在另一个线程上也遇到类似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;
}
}
`
所以上面的代码是完美的……一点都没有错。 我面对随机结果的问题是因为方法searchEmployeeRulesForSchedule(employeetype,employeedetails,scheduleId,dept,seeker,latch)
在call()下进行业务逻辑在内部调用规则引擎,该引擎未返回正确的结果,因为类的同一实例的用法,而不是每个线程的新实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.