1. 一次性任务-使用延迟
ScheduledExecutorService exec = (ScheduledExecutorService) Executors.newSingleThreadScheduledExecutor();
exec.schedule(new Thread() {
@Override
public void run() {
result.addAll(MultipleUsersRelationAssembler.retrieveRelationBeans());
}
}, TIME_BOX, TimeUnit.SECONDS);
2. 多个任务并行
//count: depends on how many task you want run Parallel
ExecutorService exec = Executors.newFixedThreadPool(count);
for (List<Integer> subList : splitedList) {
RelationJob relationJob = new RelationJob(directRelationshipService, subList, new ArrayList<MultipleUserRelationBean>());
// relationJob is runnable
exec.execute(relationJob);
}
To be continued...