简体   繁体   English

Java ExecutorService无限循环作业

[英]Java ExecutorService Infinite Loop Job

I am trying to write daemon service job in java. 我试图在java中编写守护进程服务作业。 This service will run for every minute. 这项服务将每分钟运行一次。

But I cannot implement this by using ExecutorService and I don't know if this is correct way. 但我无法通过使用ExecutorService来实现这一点,我不知道这是否正确。 Below is my code snippet: 以下是我的代码片段:

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();
    }
}

First you need to look at ScheduledExecutorService and its implementations. 首先,您需要查看ScheduledExecutorService及其实现。 This service allows you to schedule the jobs to run with pre-defined frequency. 此服务允许您安排作业以预定义的频率运行。 This is the short answer. 这是简短的答案。 As for implementation details there are too many unknowns to give you practical advice. 至于实施细节,有太多的未知数可以为您提供实用的建议。 Do you want your program to run in a container (Web or Application Server) or as Standalone with domain thread? 您希望程序在容器(Web或Application Server)中运行,还是作为具有域线程的Standalone运行? are you running on Unix/Linux (So the Cron job scheduler can be used) or Windows? 你在Unix / Linux上运行(所以可以使用Cron作业调度程序)还是Windows? One of the scheduler options could be quartz-scheduler . 其中一个调度程序选项可能是quartz-scheduler I hope this helps. 我希望这有帮助。

Your for loop does not have an ending condition : for(;;) , and no break statement. 你的for循环没有结束条件for(;;) ,没有break语句。

So all the code after this loop is if course unreachable . 所以这个循环之后的所有代码都是无法访问的


You have to wait 1 minute inside the loop , not after (as the code after your looping will never be runned). 您必须在循环内等待1分钟,而不是在循环之后(因为循环后的代码永远不会被运行)。

Keeping your synthax, I guess it should be: 保持你的synthax,我想它应该是:

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();
    service.awaitTermination(1, TimeUnit.MINUTES);
}

This here: 这里:

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
    }

Is an infinite loop; 是无限循环; it keeps submitting new jobs into your threadpool ... constantly. 它不断向你的线程池提交新的工作...... Not once per minute, but once per iteration. 不是每分钟一次 ,但每次迭代一次 You have to slow down your loop! 你必须放慢你的循环!

I am not sure what you are asking for, but you should either simply remove that loop-construct; 我不确定你要求的是什么,但你应该简单地删除那个循环结构; or more likely, do something like: 或者更可能的是,做类似的事情:

while (true) {
  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
  Thread.sleep( 1 minute );
}

or something alike. 或类似的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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