-
-
Save kdgregory/7d75e4aa6696caa7eaea2b0b77643542 to your computer and use it in GitHub Desktop.
Demonstrates the time taken for first invocation of a Java8 lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.function.Supplier; | |
/** | |
* An example that shows the time taken to invoke a Lambda for the first time. | |
*/ | |
public class Main | |
{ | |
public static void main(String[] argv) | |
throws Exception | |
{ | |
long start = System.currentTimeMillis(); | |
// uncomment one of these | |
// retryLoop(50, 250); | |
// retryLambda(50, 250, () -> doSomething()); | |
long elapsed = System.currentTimeMillis() - start; | |
System.out.println("elapsed = " + elapsed); | |
} | |
public static String retryLoop(long interval, long timeout) throws Exception | |
{ | |
long timeoutAt = System.currentTimeMillis() + timeout; | |
while (System.currentTimeMillis() < timeoutAt) | |
{ | |
String value = doSomething(); | |
if (value != null) | |
return value; | |
Thread.sleep(interval); | |
} | |
return null; | |
} | |
public static String retryLambda(long interval, long timeout, Supplier<String> lambda) throws Exception | |
{ | |
long timeoutAt = System.currentTimeMillis() + timeout; | |
while (System.currentTimeMillis() < timeoutAt) | |
{ | |
String value = lambda.get(); | |
if (value != null) | |
return value; | |
Thread.sleep(interval); | |
} | |
return null; | |
} | |
public static String doSomething() | |
{ | |
return "it doesn't matter what I return"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment