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的倍数
谢谢

解决方案 »

  1.   

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