Java固定时间间隔执行

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-29 00:04   26   0
package com.lbx.email;

import java.util.Timer;
import java.util.TimerTask;

public class Test2 {
 private final Timer timer = new Timer();
 private final int minutes;

 public Test2(int minutes) {
  this.minutes = minutes;
 }

 public void start() {
  timer.schedule(new TimerTask() {
   public void run() {
    playSound();
    timer.cancel();
    Test2 eggTimer = new Test2(2);
    eggTimer.start();
   }

   private void playSound() {
    System.out.println("Your egg is ready!");
    // Start a new thread to play a sound...
   }
  }, minutes * 1 * 1000);
 }

 public static void main(String[] args) {
  Test2 eggTimer = new Test2(1);
  eggTimer.start();
 }
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP