Last active
May 16, 2016 18:02
-
-
Save IvanVergiliev/35e685f18c62d4e691dc80dcd47ea4a4 to your computer and use it in GitHub Desktop.
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
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