我正在使用ExecutorService
,线程池为3或4.我将它们安排为scheduleWithFixedDelay
方法。我想做的是当一个任务完成我想每次运行一个新线程 。我的runnables是一个类,并没有在当前类中实现。您可以在下面找到我执行的上述任务。我无法使用CountDownLatch
,因为您无法再次实例化它。我怎么知道什么时候完成。
public class StatisticRunnable implements Runnable {
private boolean checkWaitingOnCustomer;
private int start,end;
private Main main;
public StatisticRunnable(int start, int end, boolean checkWaitingOnCustomer, Main main) {
this.start = start;
this.end = end;
this.checkWaitingOnCustomer = checkWaitingOnCustomer;
this.main = main;
}
@Override
public void run() {
main.runStatisticsInBackground(start,end, checkWaitingOnCustomer);
}
}
在我的主要班级
statisticsExecutor = Executors.newScheduledThreadPool(statisticsThreadCount);
int average = (int) (Math.ceil((usersForStatistics.size()*1d)/(statisticsThreadCount *1d)));
boolean checkKullanicidaBekliyor = false;
for(int start = 0; start < usersForStatistics.size(); start+= average){
int end =start + average - 1;
if(end > usersForStatistics.size()-1){
end = usersForStatistics.size()- 1;
checkKullanicidaBekliyor = true;
}
statisticsExecutor.scheduleWithFixedDelay(new StatisticRunnable(start,end,checkKullanicidaBekliyor,this),2,DefaultValues.statisticsRefreshTime,TimeUnit.SECONDS);
}