【问题标题】:get ScheduledExecutorService to change a variable to 3 different values获取 ScheduledExecutorService 以将变量更改为 3 个不同的值
【发布时间】:2016-03-04 16:58:20
【问题描述】:

我有任务要求在 java 上创建一个接受多个客户端的服务器程序。服务器程序只会模拟交通信号灯。将客户端连接到服务器程序不是我的问题。但是要有某种具有以下属性的调度程序:

-返回一个字符串,上面写着“红色”5 秒,“黄色”2 秒,然后“绿色”10 秒。(重复)

根据您连接到服务器的时间,您将返回其中一种颜色,就好像它是真正的红绿灯一样。

我的问题是围绕着 ScheduledExecutorService。我不是一个非常有经验的程序员,所以任何对此的解释将不胜感激。

如果我的问题难以理解,请原谅。非常感谢您! :)

【问题讨论】:

  • 您的问题很难理解,因为您没有发布问题。
  • 对不起,让我试着问一个问题:有人可以给我看一个例子,其中 ScheduledExecutorService 用于根据程序执行后的时间将变量更改为不同的值?

标签: java scheduledexecutorservice


【解决方案1】:

此链接可能会显示如何使用 ScheduledExecutorService

https://github.com/csrg-utfsm/acscb/blob/master/ACS/LGPL/CommonSoftware/jacsutil/src/alma/acs/concurrent/ThreadLoopRunner.java#L137

public ThreadLoopRunner(Runnable task, long delayTime, TimeUnit unit, final ThreadFactory tf, Logger logger, String name) {
    this.logger = logger;
    this.loopName = ( (name != null && !name.trim().isEmpty()) ? name.trim() : null );

    this.runner = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory(tf, loopName));

    this.taskWrapper = new TaskWrapper(task, loopLock, logger);
    this.delayMode = ScheduleDelayMode.FIXED_RATE;
    isDefunct = new AtomicBoolean(false);
    setDelayTime(delayTime, unit);
}

【讨论】:

    【解决方案2】:

    您可以在以下代码中找到一些想法(来自 code.openhub.net):

    public void start() {
        multicastService.get().start();
        udpService.get().start();
        connectionDispatcher.get().addConnectionAcceptor(this, false, "CONNECT", "\n\n");
        backgroundExecutor.scheduleWithFixedDelay(incomingValidator,
                timeBetweenValidates, timeBetweenValidates,
                TimeUnit.MILLISECONDS);
        _started = true;
    }
    

    http://code.openhub.net/file?fid=fPRP5s8NPWNQmdQRgi2Cl5Gh5as&cid=gm3VRNBX7Kw&s=get%20ScheduledExecutorService%20to%20change%20a%20variable%20to%203%20different%20values&pp=0&fl=Java&ff=1&filterChecked=true&fp=4419&mp,=1&ml=0&me=1&md=1&projSelected=true#L0

    【讨论】:

      猜你喜欢
      • 2020-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多