An accurate sync method that adapts automatically to the system it runs on to provide reliable results. - Java java.lang

Java examples for java.lang:Thread

Description

An accurate sync method that adapts automatically to the system it runs on to provide reliable results.

Demo Code


//package com.java2s;

public class Main {
    private static long variableYieldTime;
    private static long lastTime;

    /**//from  w w  w  .ja v a  2  s  .c  o m
     * An accurate sync method that adapts automatically to the system it runs on to provide reliable results.
     */
    public static void sync(int fps) {
        if (fps <= 0)
            return;

        long sleepTime = 1000000000 / fps; // nanoseconds to sleep this frame
        // yieldTime + remainder micro & nano seconds if smaller than sleepTime
        long yieldTime = Math.min(sleepTime, variableYieldTime + sleepTime
                % (1000 * 1000));
        long overSleep = 0; // time the sync goes over by

        try {
            while (true) {
                long t = System.nanoTime() - lastTime;

                if (t < sleepTime - yieldTime) {
                    Thread.sleep(1);
                } else if (t < sleepTime) {
                    // burn the last few CPU cycles to ensure accuracy
                    Thread.yield();
                } else {
                    overSleep = t - sleepTime;
                    break; // exit while loop
                }
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lastTime = System.nanoTime() - Math.min(overSleep, sleepTime);

            // auto tune the time sync should yield
            if (overSleep > variableYieldTime) {
                // increase by 200 microseconds (1/5 a ms)
                variableYieldTime = Math.min(
                        variableYieldTime + 200 * 1000, sleepTime);
            } else if (overSleep < variableYieldTime - 200 * 1000) {
                // decrease by 2 microseconds
                variableYieldTime = Math.max(variableYieldTime - 2 * 1000,
                        0);
            }
        }
    }
}

Related Tutorials