@FunctionalInterface public interface Metronome
Metronome instance and perform
a repeated event (perhaps using a loop), calling pause() before each event.| Modifier and Type | Method and Description |
|---|---|
static Metronome |
parker(long period,
TimeUnit unit,
Clock timeSystem)
Create a new metronome that starts ticking immediately and that uses
LockSupport.parkNanos(long) to wait. |
void |
pause()
Pause until the next tick of the metronome.
|
static Metronome |
sleeper(long period,
TimeUnit unit,
Clock timeSystem)
Create a new metronome that starts ticking immediately and that uses
Thread.sleep(long) to wait. |
void pause()
throws InterruptedException
InterruptedException - if the thread was interrupted while pausingstatic Metronome sleeper(long period, TimeUnit unit, Clock timeSystem)
Thread.sleep(long) to wait.
Generally speaking, this is a simple but inaccurate approach for periods anywhere close to the precision of the supplied
Clock (which for the system clock is typically around 10-15 milliseconds for modern Linux and OS X
systems, and potentially worse on Windows and older Linux/Unix systems. And because this metronome uses
Thread#sleep(long), thread context switches are likely and will negatively affect the precision of the metronome's
period.
Although the method seemingly supports taking TimeUnit.MICROSECONDS and TimeUnit.NANOSECONDS, it is
likely that the JVM and operating system do not support such fine-grained precision. And as mentioned above, care should
be used when specifying a period of 20 milliseconds or smaller.
period - the period of time that the metronome ticks and for which pause() waitsunit - the unit of time; may not be nulltimeSystem - the time system that will provide the current time; may not be nullstatic Metronome parker(long period, TimeUnit unit, Clock timeSystem)
LockSupport.parkNanos(long) to wait.
LockSupport.parkNanos(long) uses the underlying platform-specific timed wait mechanism, which may be more
accurate for smaller periods than sleeper(long, TimeUnit, Clock). However, like
sleeper(long, TimeUnit, Clock), the resulting Metronome may result in thread context switches.
Although the method seemingly supports taking TimeUnit.MICROSECONDS and TimeUnit.NANOSECONDS, it is
likely that the JVM and operating system do not support such fine-grained precision. And as mentioned above, care should
be used when specifying a period of 10-15 milliseconds or smaller.
period - the period of time that the metronome ticks and for which pause() waitsunit - the unit of time; may not be nulltimeSystem - the time system that will provide the current time; may not be nullCopyright © 2016 JBoss by Red Hat. All rights reserved.