[英]Java ScheduledExecutorService to run periodically
I am attempting to run a job [which contains 3 services should run parallel] for every minute . 我尝试每分钟运行一次作业(其中应并行运行3个服务) 。 Below is my code snippet.
以下是我的代码段。
ExecutorService service = Executors.newFixedThreadPool(servicesMap.size());
for (Map.Entry entry : servicesMap.entrySet()) {
service.submit(new MyService(conn, serviceID));
// here serviceID is id1 id2 id3 these three job should execute parallel
}
Note : MyTask implements Callable & servicesMap will be 3 always 注意:MyTask实现Callable&servicesMap始终为3
If i try to use ScheduledExecutorService then i cannot able to achieve it. 如果我尝试使用ScheduledExecutorService,则无法实现它。 It says scheduleService.schedule not accepting Callable type parameterts
它说scheduleService.schedule不接受Callable类型parameterts
ScheduledExecutorService scheduleService = Executors.newScheduledThreadPool(servicesMap.size());
for (Map.Entry entry : servicesMap.entrySet()) {
scheduleService.schedule((new MyService(conn, serviceID)), 0, 60, TimeUnit.SECONDS);
}
Please help to modify ScheduledExecutorService code to achieve this. 请帮助修改ScheduledExecutorService代码以实现此目的。
Your schedule(…)
call contains more arguments than the method has parameters. 您的
schedule(…)
调用包含的参数多于该方法具有的参数。
I'm not sure what exactly you are after, though. 不过,我不确定你到底在追求什么。 If you would like to run your service in one minute in the future, then use the following call:
如果您希望将来一分钟内运行服务,请使用以下电话:
scheduleService.schedule(new MyService(conn, serviceID), 60, TimeUnit.SECONDS);
If, instead, you would like to run your service every minute (starting immediately), then use one of the following two calls: 相反,如果您想每分钟运行一次服务(立即开始),请使用以下两个调用之一:
scheduleService.scheduleAtFixedRate(new MyService(conn, serviceID), 0, 60, TimeUnit.SECONDS);
scheduleService.scheduleWithFixedDelay(new MyService(conn, serviceID), 0, 60, TimeUnit.SECONDS);
Regarding your followup question in the comments: 关于您在评论中的后续问题:
How to make, within a minute Service1 Service2 Service3 should run parallel, then next minute do the same.
如何制作,在一分钟内Service1 Service2 Service3应该并行运行,然后在下一分钟做同样的事情。
ScheduledExecutorService scheduleService = …;
Collection<Callable<Void>> services = …;
Runnable svcRunner = new Runnable() {
@Override
public void run() {
Collection<Future<Void>> futures = new ArrayList<>(services.size());
// start all services in parallel
for (Callable<Void> service : services) {
// any ExecutorService would do here, i.e., doesn't have to be a
// ScheduledExecutorService
futures.add(scheduleService.submit(service));
}
// wait for all services to complete
for (Future<Void> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
// TODO do something meaningful
}
}
}
};
// run the scheduler every minute (i.e., one minute after the last service
// has finished), starting now
scheduleService.scheduleWithFixedDelay(svcRunner, 0, 60, TimeUnit.SECONDS);
For simplicity, I have stored the services in a collection. 为简单起见,我将服务存储在一个集合中。 If required, you can also create them anew each time.
如果需要,您也可以每次重新创建它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.