Skip to content

Instantly share code, notes, and snippets.

@pushkarnk
Last active October 27, 2023 06:12
Show Gist options
  • Save pushkarnk/d9e45e96f87872bdabf628d2be17c432 to your computer and use it in GitHub Desktop.
Save pushkarnk/d9e45e96f87872bdabf628d2be17c432 to your computer and use it in GitHub Desktop.
A toy program to demonstrate the C/R abilities using CRaC JDK
import java.util.*;
import java.lang.*;
import java.text.*;
import jdk.crac.*;
public class PrimeTime implements jdk.crac.Resource {
/* - get the current time in milliseconds - T1
- print the time as a human readable date
- reverse the digits in T1 to create a T2
- print the time T2 as a human readable date
*/
private int counter = 0;
public static void main(String [] args) throws Exception {
if ("warmup".equals(args[0])) {
System.out.println("New PrimeTime");
PrimeTime pt = new PrimeTime();
Core.getGlobalContext().register(pt);
pt.run();
}
}
public void run() {
while (true) {
long start = System.nanoTime();
runBenchmark();
long end = System.nanoTime();
if ( (counter < 1000 && counter % 100 == 0)
|| (counter < 5000 && counter % 500 == 0)
|| (counter < 100000 && counter % 5000 == 0)
|| (counter % 100000 == 0)) {
System.out.println("Time taken for iteration #" + counter + ": " + (end-start));
System.out.println("============================================" + Thread.currentThread());
}
counter = counter + 1;
}
}
private void runBenchmark() {
long millis = System.currentTimeMillis();
DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millis);
System.err.println("Date: " + formatter.format(calendar.getTime()));
long reverseMillis = Long.parseLong(new StringBuilder(String.valueOf(millis)).reverse().toString());
calendar.setTimeInMillis(reverseMillis);
System.err.println("Date: " + formatter.format(calendar.getTime()));
}
@Override
public void afterRestore(Context<? extends Resource> context) {
counter = 0;
}
@Override
public void beforeCheckpoint(Context<? extends Resource> context) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment