java 8 adjustable time scheduler

时间:2016-10-20 18:38:50

标签: java

I need to implement the following functionality: I have something like a messenger. So, user sends messages, and system should wait 3 sec after last message. So, for example user sends 'Hello!',and system should start countdown, and if 2 seconds passed and user sends 'hey!', it should start counting down again. If 3 secs has passed, special action should fire. Are there any special classes and technologies for this in java and java frameworks?

UPDATE:

Basically I need something ready to implement a time barrier with ability to change waiting time. So, I need to wait 3 secs after LAST message came. So, when API gets message 'Hello', it should start waiting for 3 secs before perform some action. If 2 secs passed and API gets next message 'Hey', it should init it's time counter and start counting down seconds again. So, totally 5 secs are passed. And If there's no message to API for 3 secs after last message, action should run.

If I do Thread.sleep or future.get I specify fixed number of secs to delay, but I want to have ability to change this number online and system should take it. For example:

int counter=5;//any shared seconds quantity holder

while(true){
  counter--;
  Thread.sleep(1000);
}

....

setCounter(5);//I call somewhere, and loop will be looping again. 

But I am looking for something stable and tested, not to invent a bicycle.

0 个答案:

没有答案