我有一个变量,其值需要根据条件每 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/