爲什麼不直接致電ScheduledExecutorService.scheduleAtFixedRate或ScheduledExecutorService.scheduleWithFixedDelay?
UPDATE
這是實現什麼(我相信)你想要的手段之一:(?性能問題)
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
void start(final Connection conn)
{
executor.scheduleWithFixedDelay(new Runnable(){ public void run(){ try { poll(conn); } catch (Exception e) { e.printStackTrace(); } } }, 0, 1, TimeUnit.HOURS);
}
private void poll(Connection conn) throws SQLException
{
final ResultSet results = conn.createStatement().executeQuery("SELECT song, playtime FROM schedule WHERE playtime > GETDATE() AND playtime < GETDATE() + 1 HOUR");
while (results.next())
{
final String song = results.getString("song");
final Time time = results.getTime("playtime");
executor.schedule(new Runnable(){ public void run() { play(song); } }, time.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
}
}
Hii.Yeah但後來我不得不一次創建10000調度。此外,我的數據庫不斷更新,所以每1小時輪詢一次數據庫,然後在1小時內安排所有歌曲,這正是我想要的。沒有可能嗎? – user2511713
@ user2511713,爲什麼每首歌曲需要一個調度程序? – SimonC
謝謝,這是我想要的。:-) – user2511713