import java.util.Timer;
import java.util.TimerTask; public class EggTimer { //每隔1秒就打印 private final Timer timer = new Timer(); int i; private final int minutes; public EggTimer(int minutes) { this.minutes = minutes; } public void start() { timer.schedule(new TimerTask() { public void run() { print(); if(i==6) // System.exit(0); timer.cancel(); } private void print() { System.out.println("Your egg is ready!"+i); i++; } }, minutes * 0,1000);//如果就打算执行一次,就把最后参数去掉 } public static void main(String[] args) { EggTimer eggTimer = new EggTimer(1); eggTimer.start(); } } |
timer类的使用
最新推荐文章于 2021-03-09 10:29:15 发布