Java:addScheduledJobAfterDelay() - 我可以强制启动计划作业吗?

发布于 2024-08-31 09:20:14 字数 153 浏览 8 评论 0原文

我正在开发一款扑克游戏。在投注阶段,我使用 Red5 iSchedulingService 创建一个计划作业,该作业每 8 秒运行一次,转发给下一个玩家进行投注。现在,如果用户在 8 秒结束之前下注,我想手动强制启动下一个计划作业。

有没有办法强制预定的作业在需要时立即开始?

I'm developing a poker game. On the betting stage I create a scheduled job using Red5 iSchedulingService that will run every 8 seconds to forward to the next player to place a bet. Now if the user placed a bet before the 8 seconds are over, I want to manually force the next scheduled job to start.

Is there a way to force the scheduled job to start immediately when required?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞清仓 2024-09-07 09:20:14

您可以使用 执行器。有更干净的实现,但这是一个尝试和一些基本的东西,可以使用 未来可调用

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

You can do this with Executors. There are cleaner implementations, but this is a stab and something basic that does what you want using Future and 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;
    }
}
心不设防 2024-09-07 09:20:14

我在这个线程中开始的具体问题的答案:

我不能强制启动计划作业,但我可以做的是删除计划作业并延迟 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());

an answer to my specific question that I started with in this thread:

i cannot force to start a scheduled job, but what I can do is remove the scheduled job and start a new job with a delay of 0 seconds.

addScheduledJobAfterDelay() return a string that represents the job id. i can use it to remove the scheduled job. The problem is that there is no way to know if I'm interrupting the scheduled job or not. Executors do provide that information. that is why Executors are better choice in this specific case then using the red5 scheduling service.

how to remove a scheduled job (red5):

ISchedulingService scheduler = (ISchedulingService) getScope().getContext().getBean(ISchedulingService.BEAN_NAME);
     scheduler.removeScheduledJob("ScheduleJobString");

the string ScheduleJobString should be replaced with the string that you have received from creating the job:

String scheduleJobString = scheduler.addScheduledOnceJob(DelayInSeconds*1000,new MyJob());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文