使用ExecutorService计划的任务

Ahm*_*dhc 4 java scheduled-tasks

我想ExecutorService在JAVA中使用每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*
      }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Boz*_*zho 7

ScheduledExecutorService service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(command, 5, 5, TimeUnit.MINUTES);
Run Code Online (Sandbox Code Playgroud)

在哪里command:

Runnable command = new Runnable() {
   public void run() {
      // update database
   }
}
Run Code Online (Sandbox Code Playgroud)