Java. Difference between Thread.sleep() and ScheduledExecutorService methods

101 views Asked by At

I'm trying to make a clock desktop app and was using

Thread.sleep(1000);

inside a while(true) loop. When I was wondering how this works and might affect CPU performance, I found that ScheduledExecutorService is more preferable and claimed by some StackOverflowers to be more accurate.

What's the difference between them?

(also saw another option using swing timers, but more curious about "advanced" stuff)

EDIT: To be more specific:

From: Java 7 doc

The scheduleAtFixedRate and scheduleWithFixedDelay methods create and execute tasks that run periodically until cancelled.

Is there any difference between Thread.sleep() and these 2 methods? (threads usage, CPU consumption, precision, when is better to use one or another, etc)

0

There are 0 answers