简体   繁体   English

Java ScheduledExecutorService定期运行

[英]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.

相关问题 定期运行ScheduledExecutorService或根据条件运行一次 - Run a ScheduledExecutorService periodically or just once based on condition 使用ScheduledExecutorService方法定期运行一批任务 - Using ScheduledExecutorService method to run a batch of tasks periodically 如何在启动Apache tomcat服务器而不是使用ScheduledExecutorService的Java类时定期运行javascript函数? - How to run javascript function periodically when the Apache tomcat server is starts instead of java class using ScheduledExecutorService? 在后台运行Java ScheduledExecutorService - Run Java ScheduledExecutorService in the background 如何使用ScheduledExecutorService通过多个服务定期运行作业 - How to use ScheduledExecutorService to run the job periodically with multiple services 使用ScheduledExecutorService在Java中定期运行任务 - Using a ScheduledExecutorService to run a task on a periodic basis in Java 通过ScheduledExecutorService定期运行Callable - Periodically running a Callable via ScheduledExecutorService 在App Engine上定期运行一些Java? - Run some java periodically on App Engine? 在Java中定期运行任务的更好方法 - A better way to run task periodically in Java 使用ScheduledExecutorService的Java内存泄漏 - Java Memory Leak with ScheduledExecutorService
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM