(使用newScheduledThreadPool来模拟心跳机制)

使用newScheduledThreadPool来模拟心跳机制
 1 public class HeartBeat {
 2     public static void main(String[] args) {
 3         ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
 4         Runnable task = new Runnable() {
 5             public void run() {
 6                 System.out.println("HeartBeat.........................");
 7             }
 8         };
 9         executor.scheduleAtFixedRate(task,5,3, TimeUnit.SECONDS);   //5秒后第一次执行,之后每隔3秒执行一次
10     }
11 }
使用newScheduledThreadPool来模拟心跳机制
输出:
HeartBeat....................... //5秒后第一次输出
HeartBeat....................... //每隔3秒输出一个

相关文章:

  • 2021-09-23
  • 2021-06-20
  • 2021-07-30
  • 2021-08-30
  • 2021-09-18
  • 2021-07-19
  • 2021-12-02
  • 2021-08-08
猜你喜欢
  • 2021-10-19
  • 2021-10-19
  • 2021-06-21
相关资源
相似解决方案