java - 每 5 分钟更新一次变量的值

标签 java multithreading parallel-processing

我有一个变量,其值需要根据条件每 5 分钟更新一次。我知道我需要启动一个不同的线程。但我怎样才能做到这一点呢?

最佳答案

使用ScheduledThreadPoolExecutor ,如下图(基于this code):

public class Main{
 public static void main(String args[]) {
   ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(2);    
   stpe.scheduleAtFixedRate(new YourJob(), 0, 5, TimeUnit.MINUTES);
 }
}

class YourJob implements Runnable {
   public void run() {
   // your task
    System.out.println("Job 1");
  }
}

关于java - 每 5 分钟更新一次变量的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19518348/

相关文章:

java - 如何在Spring中创建Service类对象

multithreading - C# 如何最大限度地提高特定代码部分没有上下文切换的机会?

parallel-processing - 在 makefile 中获取当前作业编号

r - R中使用foreach时是否导入 "parallel"包

java - 从 Android 应用程序将数据存储在 Excel 文件中

java - 使用带有 JCheckBox 的 itemListener 来显示/隐藏 JTextField

Android Looper.prepare() 和 AsyncTask

multithreading - coldfusion 11后台处理程序和cfmail线程

bash - GNU 并行 : how to format replacement string?

java - Java双向链表