Skip to content

Instantly share code, notes, and snippets.

@bzdgn
Last active August 29, 2015 14:23
Show Gist options
  • Save bzdgn/5f774fddb1e6a9334aaa to your computer and use it in GitHub Desktop.
Save bzdgn/5f774fddb1e6a9334aaa to your computer and use it in GitHub Desktop.
A Checked Sleep Method versus Thread.sleep() Method for Consistency - Enhanced Version
package com.levent.experimental.sleep;
/*
* Because that the consistency of the precision of Thread.sleep method
* is not guaranteed, a container method (delayNSeconds) is tested if
* the duration reached with the Thread.sleep method or not.
*
* On this simple dummy code, Thread.sleep method is compared with the
* container method for countTest times.
*
* On this enhanced container version, the container method recalculates
* each time to estimate the remaining delta time.
*
* A simple output of this gist is as below
*
*
* Test Method vs. Sleep for 500 times;
* Delay period: 20
* Final Results
* *************
* Sleep wins : 0
* Method wins : 500
* Equality : 0
*
* @Author: Levent Divilioglu
*
*/
public class TestSleepMethodEnhanced {
public static void main(String[] args) throws Exception {
final long delay = 2;
final int loopCount = 10;
final int testCount = 500;
// compareDelayMethodologies(delay, loopCount);
countCompareDelayMethodologies( testCount, delay, loopCount );
}
private static boolean delayNSeconds( long milliseconds ) {
long t2 = System.currentTimeMillis() + milliseconds;
long remaining = milliseconds;
while( remaining <= 0 ) {
try {
Thread.sleep(remaining);
remaining = t2 - System.currentTimeMillis();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return true;
}
private static void testDelayWithSleep(long milliSeconds) {
// System.out.println( "...CurrentMillis: " + System.currentTimeMillis() );
try {
Thread.sleep(milliSeconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
// System.out.println( "...CurrentMillis: " + System.currentTimeMillis() );
}
private static void testDelayWithDelayNSeconds(long milliSeconds) {
// System.out.println( "...CurrentMillis: " + System.currentTimeMillis() );
delayNSeconds(milliSeconds);
// System.out.println( "...CurrentMillis: " + System.currentTimeMillis() );
}
private static int[] compareDelayMethodologiesToConsole(long delay, long loopCount) {
long period = delay*loopCount;
long errorSleep;
long errorMethod;
int results[] = { 0, 0, 0 };
System.out.println("Test of Thread.sleep precision");
System.out.println("******************************");
long tStart = System.currentTimeMillis();
for(int i = 0; i < loopCount; i++) {
testDelayWithSleep(delay);
}
long delta = System.currentTimeMillis() - tStart;
errorSleep = delta-period;
System.out.println("Period: " + period + "\tDelta: " + delta + "\tError: " + errorSleep);
System.out.println();
System.out.println("Test of delayNSeconds precision");
System.out.println("*******************************");
tStart = System.currentTimeMillis();
for(int i = 0; i < loopCount; i++) {
testDelayWithDelayNSeconds(delay);
}
delta = System.currentTimeMillis() - tStart;
errorMethod = delta-period;
System.out.println("Period: " + period + "\tDelta: " + delta + "\tError: " + errorMethod);
System.out.printf("\nPrecise Winner: ");
if( errorMethod < errorSleep ) {
System.out.printf(" Method\n");
results[2]++;
}
else if ( errorMethod == errorSleep ) {
System.out.printf(" - \n");
results[0]++;
}
else {
System.out.printf(" Thread.sleep\n");
results[1]++;
}
return results;
}
private static int[] compareDelayMethodologies(long delay, long loopCount) {
long period = delay*loopCount;
long errorSleep;
long errorMethod;
int results[] = { 0, 0, 0 };
long tStart = System.currentTimeMillis();
for(int i = 0; i < loopCount; i++) {
testDelayWithSleep(delay);
}
long delta = System.currentTimeMillis() - tStart;
errorSleep = delta-period;
tStart = System.currentTimeMillis();
for(int i = 0; i < loopCount; i++) {
testDelayWithDelayNSeconds(delay);
}
delta = System.currentTimeMillis() - tStart;
errorMethod = delta-period;
if( errorMethod < errorSleep )
results[2]++;
else if ( errorMethod == errorSleep )
results[0]++;
else
results[1]++;
return results;
}
private static void countCompareDelayMethodologies(int testCount, long delay, long loopCount) {
int[] finalResults = { 0, 0, 0 }; // 0: equality, 1:sleep, 2:method
int[] temp;
for( int i = 0; i < testCount; i++ ) {
temp = compareDelayMethodologies(delay, loopCount);
finalResults[0] += temp[0];
finalResults[1] += temp[1];
finalResults[2] += temp[2];
}
System.out.println("Test Method vs. Sleep for " + testCount + " times;");
System.out.println("Delay period: " + loopCount*delay);
System.out.println();
System.out.println("Final Results");
System.out.println("*************");
System.out.println("Sleep wins : " + finalResults[1]);
System.out.println("Method wins : " + finalResults[2]);
System.out.println("Equality : " + finalResults[0]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment