简体   繁体   English

获取ScheduledExecutorService将变量更改为3个不同的值

[英]get ScheduledExecutorService to change a variable to 3 different values

Im having task where im asked to make a server program on java that accepts multiple clients. 我正在执行任务,要求我在Java上制作一个接受多个客户端的服务器程序。 The server program is going to do nothing more than simulate a traffic light. 该服务器程序除了模拟交通信号灯外,不会做其他任何事情。 Connecting the client to the server program is not my issue. 将客户端连接到服务器程序不是我的问题。 But to have some kind of scheduler with the following properties: 但是要拥有某种具有以下属性的调度程序:

-Returns a String that says "Red" for 5 sec, "Yellow" for 2 seconds and then "green" for 10 seconds.(repeat) -返回一个字符串,该字符串说“红色” 5秒钟,“黄色” 2秒钟,然后“绿色” 10秒钟。(重复)

Depending on when you connect to the server you will get back one of this colors as if it was a real traffic light. 根据连接服务器的时间,您将获得一种颜色,就好像它是真实的交通信号灯一样。

my problem is wrapping my head around ScheduledExecutorService. 我的问题是把我的头围绕ScheduledExecutorService。 Im not a very experienced programmer, so any explanation around this would be highly appreciated. 我不是一个非常有经验的程序员,所以对此的任何解释将不胜感激。

Excuse me if my question is hard to understand. 如果我的问题很难理解,请原谅。 Thank you so much in advance! 提前非常感谢您! :) :)

This link might show how to use ScheduledExecutorService : 该链接可能显示如何使用ScheduledExecutorService

https://github.com/csrg-utfsm/acscb/blob/master/ACS/LGPL/CommonSoftware/jacsutil/src/alma/acs/concurrent/ThreadLoopRunner.java#L137 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);
}

You can find some ideas in the following code (from code.openhub.net): 您可以在以下代码中找到一些想法(来自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 http://code.openhub.net/file?fid=fPRP5s8NPWNQmdQRgi2Cl5Gh5as&cid=gm3VRNBX7Kw&s=get%20ScheduledExecutorService%20to%20change%20a%20variable%20to%203%20different%20values&pp=1&f=ed=&filter=Java&ff= = 0&me = 1&md = 1&projSelected = true#L0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM