Skip to content

Instantly share code, notes, and snippets.

@IvanVergiliev
Last active May 16, 2016 18:02
Show Gist options
  • Save IvanVergiliev/35e685f18c62d4e691dc80dcd47ea4a4 to your computer and use it in GitHub Desktop.
Save IvanVergiliev/35e685f18c62d4e691dc80dcd47ea4a4 to your computer and use it in GitHub Desktop.
package io.github.ivanvergiliev;
public class NonVolatileFlag {
private static boolean flag = false;
public static class FlagChecker implements Runnable {
public void run() {
long iter = 0;
while (flag == false) {
++iter;
// Wait and do nothing.
}
}
}
public static class FlagSetter implements Runnable {
public void run() {
int count = 0;
while (true) {
++count;
if (count > 100_000) {
break;
}
}
flag = true;
}
}
public static void main(String[] args) throws InterruptedException {
for (int i = 0; ; ++i) {
System.out.println("Running iteration " + i);
flag = false;
Thread checker = new Thread(new FlagChecker());
Thread setter = new Thread(new FlagSetter());
checker.start();
setter.start();
checker.join();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment