Skip to content

Instantly share code, notes, and snippets.

@alf239
Created January 7, 2012 11:43
Show Gist options
  • Select an option

  • Save alf239/1574533 to your computer and use it in GitHub Desktop.

Select an option

Save alf239/1574533 to your computer and use it in GitHub Desktop.
public class VolatileExample implements Runnable {
public static boolean flag = true; // do not try this at home
public void run() {
long i = 0;
while (flag) {
if (i++ % 10000000000L == 0)
System.out.println("Waiting " + System.currentTimeMillis());
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new VolatileExample());
thread.start();
Thread.sleep(10000L);
flag = false;
long start = System.currentTimeMillis();
System.out.println("stopping " + start);
thread.join();
long end = System.currentTimeMillis();
System.out.println("stopped " + end);
System.out.println("Delay: " + ((end - start) / 1000L));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment