我正嘗試在java中編寫守護程序服務作業。這項服務將每分鐘運行一次。Java ExecutorService無限循環作業
但我不能通過使用ExecutorService實現這一點,我不知道這是否是正確的方式。下面是我的代碼片段:
public void startService() {
try {
ExecutorService service = Executors.newFixedThreadPool(3);
for (;;) {
service.submit(new Service1()); // this will send some set of emails
service.submit(new Service2()); // this will send some set of emails
service.submit(new Service3()); // this will send some set of sms
}
service.shutdown(); // It says Unreachable code so when should i shutdown the service
service.awaitTermination(1, TimeUnit.MINUTES);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
是的,它沒有結束工作。它會每分鐘運行一次。你能說說怎麼做到這一點。 – DEADEND