繁体   English   中英

使用ExecutorService计划的任务

[英]Scheduled Task using ExecutorService

我想在JAVA中使用ExecutorService来安排每5分钟插入数据库。 这是我想要执行它的任务:

    MyClass{
     a counter that count a handled data received from a Thread
     I receive usually 300 data line/second
     Apply a treatment on these data and keep the results in the memory

    every five minutes {
               update the database with the counters saved in memory*
      }
}

基本上,每当他从后台运行的线程获取数据时,它就会调用该任务。 因为我有超过300个数据/秒,所以不可能以这种方式使用它。

所以我正在尝试做id来处理收到的任务并在内存中保留一个计数器并且每5分钟更新一次数据库。

我的问题是,是否可以使用这些java函数ScheduledExecutorService来做到这一点,我们怎么做呢(我不想在任务上阻止我的JAVA应用程序5min,我希望该应用程序正常运行但不执行每次都有任务,如果你能告诉我一些这些功能的用法,我很感激吗?

ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(command, 5, 5, TimeUnit.MINUTES);

其中command是:

Runnable command = new Runnable() {
   public void run() {
      // update database
   }
}

暂无
暂无

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

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