Java未来。它在调试模式下成功,但在我正常运行时失败
伙计们,我也面临着类似的情况 喜欢 虽然我没有junit的案子。我尝试了我所知道的一切。。在链接页面上包括建议,保持倒计时和线程睡眠,但结果不会改变。如果我运行debug并给它一些时间,它会显示所有线程的所有结果,但是如果我正常运行它,它总是给我更少的结果。 我的代码如下 `Java未来。它在调试模式下成功,但在我正常运行时失败,java,multithreading,debugging,future,callable,Java,Multithreading,Debugging,Future,Callable,伙计们,我也面临着类似的情况 喜欢 虽然我没有junit的案子。我尝试了我所知道的一切。。在链接页面上包括建议,保持倒计时和线程睡眠,但结果不会改变。如果我运行debug并给它一些时间,它会显示所有线程的所有结果,但是如果我正常运行它,它总是给我更少的结果。 我的代码如下 ` AtomicInteger-AtomicInteger=新的AtomicInteger(employeeids.size()); CountDownLatch latch=新的CountDownLatch(employee
AtomicInteger-AtomicInteger=新的AtomicInteger(employeeids.size());
CountDownLatch latch=新的CountDownLatch(employeeId.size());
Iterable batchList=CreateBacks(EmployeeId,batchSize);
Set=newhashset();
对于(列表employeeidsList:batchList){
Callable Callable=新的ScheduleCallable(employeetype、employeedetails、employeeidsList、dept、seeker、atomicInteger、latch);
Future=pool.submit(可调用);
集合。添加(未来);
试一试{
睡眠(1000);
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
试一试{
wait(getTimeOutInMillis(),TimeUnit.millists);
}捕捉(中断异常e1){
//TODO自动生成的捕捉块
抛出new EmployeeException(“时间表的构建没有在[“+getTimeOutInMillis()+“]millis.”的时间内完成);
}
long timeLeft=getTimeOutInMillis();
布尔检查=真;
while(检查){
debug(“正在等待构建资产。倒计时值为[“+timeLeft+”]);
试一试{
睡眠(暂停的时间);
timeLeft=timeLeft-暂停时间;
if(timeLeft==0 | | timeLeft<0){
抛出new EmployeeException(“时间表的构建没有在[“+getTimeOutInMillis()+“]millis.”的时间内完成);
}
for(未来:集合){
如果(!future.isDone()){
检查=正确;
打破
}
else{check=false;}
}
}捕捉(中断异常e){
logger.error(“等待资产生成bulid时出错”);
}
}
for(未来:集合){
试一试{
EmployeeScheduleList.addAll(future.get());
}捕捉(中断异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(执行例外){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
公共静态类ScheduleCallable实现了可调用
{
私有字符串employeetype;
私人名单雇员详情;
私人名单雇员名单;
私人弦乐部;
私人雇主求职者助手求职者;
私有原子整数原子整数;
私人倒计时闩锁;
公共可调度调用(字符串employeetype、列表employeedetails、,
列表employeeidsList、字符串部门、EmployeeSeekerHelper seeker、AtomicInteger
原子整数,倒计时锁存器(锁存器)
{
this.employeetype=employeetype;
this.employeedetails=employeedetails;
this.employeeidsList=employeeidsList;
this.dept=dept;
this.seeker=seeker;
this.atomicInteger=atomicInteger;
这个.闩锁=闩锁;
}
公共列表调用()
{
List EmployeeScheduleList=new ArrayList(0);
int计数器=1;
for(字符串scheduleId:employeeidsList)
{
倒计时();
addAll(searchEmployeeRulesForSchedule(employeetype,employeedetails,scheduleId,dept,seeker,latch));
System.out.println(“线程计数器”+计数器);
atomicInteger.decrementAndGet();
计数器++;
//倒计时();
}
返回员工时间表;
}
}
`所以上面的代码非常好。。。一点问题也没有。我面对随机结果的问题是因为方法
searchEmployeeRulesForSchedule(employeetype,employeedetails,scheduleId,dept,seeker,latch)
call()下的业务逻辑在内部调用一个规则引擎,该引擎没有返回正确的结果,因为使用了类的同一实例,而不是每个线程的新实例 TL;DR你能指出你遇到的问题并创建一小段代码来复制它吗?+1到注释中,如果你指定了失败的地方,它也会有所帮助。您是否遇到异常,如果是,是什么?您是否在某个地方得到了意外的结果?如果是,它们是什么?您期望得到什么?假设我的批处理列表大小为5,每个对象包含10个EmployeeID。所以batchlist循环应该运行5次,我不希望结果来自这5个线程。。但我看到的是,我有时会从1个线程随机得到结果,有时会从2个线程随机得到结果。我的cord尝试的工作方式是在每个batchlist对象(employeeid)的call()方法中运行另一个循环来调用实际执行业务逻辑的方法。
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;
}
}