2016-03-04 77 views
0

我有一個任務,其中im要求在接受多個客戶端的java上創建服務器程序。服務器程序只會模擬交通信號燈。將客戶端連接到服務器程序不是我的問題。但有某種調度器具有以下屬性:get ScheduledExecutorService將變量更改爲3個不同的值

- 返回一個字符串,上面寫着「紅」,持續5秒,「黃」 2秒鐘,然後「綠色」,持續10秒(重複)

根據您何時連接到服務器,您將獲得這種顏色之一,就好像它是一個真正的交通燈。

我的問題是圍繞ScheduledExecutorService進行包裝。我不是一個非常有經驗的程序員,所以任何關於此的解釋將不勝感激。

對不起,如果我的問題很難理解。提前感謝你! :)

+0

你的問題很難理解,因爲你沒有發佈問題。 – jheimbouch

+0

對不起,讓我試試在一個問題中提問: 有人可以告訴我一個例子,其中ScheduledExecutorService用於在程序執行後根據時間將變量更改爲不同的值嗎? –

回答

0

此鏈接可能會展示如何使用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); 
} 
0

您可以在下面的代碼的一些想法(從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