我正在開發一款撲克遊戲。在投注階段,我使用Red5 iSchedulingService創建一個計劃任務,每8秒運行一次,轉發給下一個玩家下注。現在,如果用戶在8秒鐘之前下注,我想手動強制下一個預定作業開始。Java:addScheduledJobAfterDelay() - 我可以強制執行預定作業嗎?
有沒有辦法強制計劃的工作立即啓動時,需要?
我正在開發一款撲克遊戲。在投注階段,我使用Red5 iSchedulingService創建一個計劃任務,每8秒運行一次,轉發給下一個玩家下注。現在,如果用戶在8秒鐘之前下注,我想手動強制下一個預定作業開始。Java:addScheduledJobAfterDelay() - 我可以強制執行預定作業嗎?
有沒有辦法強制計劃的工作立即啓動時,需要?
回答我的具體問題,我在此線程開始:
我不能強迫啓動計劃作業,但我可以做的就是刪除預定工作,開始了新的工作延遲0秒。
addScheduledJobAfterDelay()返回一個表示作業ID的字符串。我可以用它來刪除預定的工作。問題是沒有辦法知道我是否打斷了預定的工作。執行者提供這些信息。這就是爲什麼執行者在這種特殊情況下更好的選擇,然後使用red5調度服務。
如何刪除計劃作業(RED5):
ISchedulingService scheduler = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
scheduler.removeScheduledJob("ScheduleJobString");
字符串ScheduleJobString
應與您從創建工作收到的字符串替換:
String scheduleJobString = scheduler.addScheduledOnceJob(DelayInSeconds*1000,new MyJob());
你可以用Executors來做到這一點。有更清潔的實施,但這是一個刺和基本的東西,你想要使用Future和Callable。
// wherever you set up the betting stage
ScheduledExecutorService bettingExecutor =
Executors.newSingleThreadScheduledExecutor();
ScheduledFuture<?> future = bettingExecutor.schedule(new BettingStage(), 8,
TimeUnit.SECONDS);
//...
// in the same class (or elsewhere as a default/protected/public class)
private class BettingStage implements Callable<ScheduledFuture<?>>() {
public ScheduledFuture<?> call() thows ExecutionException {
ScheduledFuture<?> future = bettingExecutor.schedule(new BettingStage(), 8,
TimeUnit.SECONDS);
// betting code here
boolean canceled = future.cancel(false); // cancels the task if not running yet
if(canceled) {
// run immediately
future = bettingExecutor.schedule(new BettingStage(),
0, TimeUnit.SECONDS)
}
return future;
}
}
這是第一次我聽說過iSchedulingService。我懷疑這是否是在同伴們中間廣爲人知的。如果你使用Quartz或者內建的'java.util.concurrent.Executors'或者''java.util.Timer',你可能在這裏有更多的答案。 – BalusC 2010-05-05 14:33:18
您使用的是什麼JDK? – justkt 2010-05-11 12:08:11
1.5雪豹 – ufk 2010-05-11 12:33:38