, , :
public class Wave extends Thread {
private final Object BLOCKER = new Object();
public Wave() {
super();
setPriority(MAX_PRIORITY);
}
@Override
public void run() {
for (;;) {
Wave.high();
long t1 = System.currentTimeMillis();
LockSupport.parkUntil(BLOCKER, t1 + 20 - (t1 % 20));
Wave.low();
long t2 = System.currentTimeMillis();
LockSupport.parkUntil(BLOCKER, t2 + 20 - (t2 % 20));
}
}
public static native void high();
public static native void low();
}
Since it depends on the wall clock's time ms, rather than the more accurate time in nanoseconds, it will not work at much higher frequencies. But this may not work, since GC (and other processes) can interrupt this thread for an βunsuccessfulβ period of time, which will lead to the same jitter.
When I tested this on my Windows 7 quad-core processor with JDK 6, I had some kind of minor jitter about every second, so the aix solution is probably better
source
share