loops - Resuming a Java program when the computer sleeps? -
i have java program has loop in checks new messages every twenty seconds.
example:
while (true) { thread.sleep(20000); newmessages(); }
this works when computer being run. however, if user put computer sleep , wake duration of time later, whole program gets messed , no longer checks messages every twenty seconds. there way correct this?
instead of using thread.sleep
can use timer
.
a facility threads schedule tasks future execution in background thread. tasks may scheduled one-time execution, or repeated execution @ regular intervals.
your code might be:
final timer timer = new timer(); final timertask task = new timertask() { @override public void run() { newmessages(); } }; timer.scheduleatfixedrate(task, new date(), 20000);
Comments
Post a Comment