62,614
社区成员
发帖
与我相关
我的任务
分享
public class Test {
public static void main(String[] args) {
ScheduledExecutorService schedule = Executors.newScheduledThreadPool(1);
Beeper b = new Beeper();
schedule.scheduleAtFixedRate(b, 0, b.DELAY, b.TIME_UNIT);
}
}
class Beeper implements Runnable {
private static int times = 1;
private static int count = 0;
public final int DELAY = 2;
public final TimeUnit TIME_UNIT = TimeUnit.SECONDS;
@Override
public void run() {
// TODO Auto-generated method stub
if(count == times) {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
System.out.println(format.format(new Date())+ " 第 "+times+" 次运行,距离上次 "+(DELAY*times) +" 秒");
count = 1;
times++;
}else {
count++;
}
}
}