请教ScheduledExecutorService 刷新时间间隔的问题

wtobias 2011-05-18 11:41:56
ScheduledExecutorService scheduler;
int repollTimes;
scheduler = Executors .newScheduledThreadPool(1);
Runnable beeper = new Runnable() {
public void run() {
......
}
};
final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 15, 15*(repollTimes+1), TimeUnit.SECONDS);
scheduler.schedule(new Runnable() {
public void run() {
beeperHandle.cancel(true);
scheduler.shutdown();
}
}, 3000, TimeUnit.SECONDS);
问题是我想刷新时间是以15的倍数递增的15*(repollTimes+1),可按照上面的代码,只能是15后开始间隔15秒,谁能说一下怎么能实现15秒后开始间隔15的倍数
谢谢
...全文
298 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
andycpp 2011-05-19
  • 打赏
  • 举报
回复
15秒太多,我这个例子是按照2秒的倍数来递增的,你可以根据自己的需要修改

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++;
}
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧