2010-05-05 66 views
1

我正在开发一款扑克游戏。在投注阶段,我使用Red5 iSchedulingService创建一个计划任务,每8秒运行一次,转发给下一个玩家下注。现在,如果用户在8秒钟之前下注,我想手动强制下一个预定作业开始。Java:addScheduledJobAfterDelay() - 我可以强制执行预定作业吗?

有没有办法强制计划的工作立即启动时,需要?

+2

这是第一次我听说过iSchedulingService。我怀疑这是否是在同伴们中间广为人知的。如果你使用Quartz或者内建的'java.util.concurrent.Executors'或者''java.util.Timer',你可能在这里有更多的答案。 – BalusC 2010-05-05 14:33:18

+0

您使用的是什么JDK? – justkt 2010-05-11 12:08:11

+0

1.5雪豹 – ufk 2010-05-11 12:33:38

回答

1

回答我的具体问题,我在此线程开始:

我不能强迫启动计划作业,但我可以做的就是删除预定工作,开始了新的工作延迟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()); 
1

你可以用Executors来做到这一点。有更清洁的实施,但这是一个刺和基本的东西,你想要使用FutureCallable

// 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; 
    } 
} 
+0

非常感谢!去了解它。 – ufk 2010-05-05 15:09:59

+0

如何获取可调用类中的bettingExecutor? – ufk 2010-05-11 09:41:33

+0

在你的例子中,你创建了一个新的时间表,并立即取消它。所以..它永远不会被执行不是? – ufk 2010-05-11 09:52:48

相关问题