简体   繁体   中英

Java ScheduledExecutorService to run periodically

I am attempting to run a job [which contains 3 services should run parallel] for every minute . 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

If i try to use ScheduledExecutorService then i cannot able to achieve it. It says scheduleService.schedule not accepting Callable type 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.

Your schedule(…) call contains more arguments than the method has parameters.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM