我正在开发一款扑克游戏。在投注阶段,我使用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