我想安排一个任务一开始经常发生,然后随着时间的推移减少频率。我不希望仅仅为此添加对 Quartz 的依赖项。
使用标准 Java 库,我想出的最好的方法是一系列一次性任务,然后是频率较低的线性计划:
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
myRunnable = new MyRunnable();
executor.schedule( myRunnable, 0, TimeUnit.SECONDS );
executor.schedule( myRunnable, 5, TimeUnit.SECONDS );
executor.schedule( myRunnable, 10, TimeUnit.SECONDS );
executor.schedule( myRunnable, 15, TimeUnit.SECONDS );
executor.schedule( myRunnable, 30, TimeUnit.SECONDS );
executor.schedule( myRunnable, 45, TimeUnit.SECONDS );
executor.scheduleWithFixedDelay( myRunnable, 60, 300, TimeUnit.SECONDS );
有没有更优雅的方法?