2011-04-05 19 views
0

我都需要計時器代碼在Java中使用這些功能:簡單的Java定時器庫

  • 設置碼片(Runnable或類似的東西)特定的延遲後執行一次(與第二精度)
  • 更改事件後取消定時器的現有定時器的延遲。
  • 使用線程池(所以沒有java.util.Timer

我已經研究過使用Quartz,但它似乎是太大了。

這是否有一個小型庫?或者我真的可以使用Quartz?或者我會如何使用ScheduledExecutor來實現它?

回答

1

看看這個帖子:Java Timer vs ExecutorService?

public <V> ScheduledFuture<V> schedule(Callable<V> callable, 

       long delay, TimeUnit unit); 

public ScheduledFuture<V> scheduleAtFixedRate(Runnable command, 

       long initialDelay, long period, TimeUnit unit); 

public ScheduledFuture<V> scheduleWithFixedDelay(

       Runnable command, long initialDelay, 

       long delay, TimeUnit unit); 
+0

這是我現在使用的,但我需要能夠更改現有計時器的延遲才能繼續。 – 2011-04-05 11:08:27

+0

@Bart:好的,我明白你想要動態改變延遲。 – 2011-04-05 11:26:42