Skip to content

Instantly share code, notes, and snippets.

@CyberFlameGO
Created October 20, 2021 05:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CyberFlameGO/81df6ff2263697eb3a577e25aca6a68f to your computer and use it in GitHub Desktop.
Save CyberFlameGO/81df6ff2263697eb3a577e25aca6a68f to your computer and use it in GitHub Desktop.
Java volatility
public class VolatileExample {
/*
ESSENTIALLY, WITHOUT VOLATILE THREADS OTHER THAN MAIN WON'T BE ABLE
TO FETCH THE MOST UPDATED VALUE FOR THIS VARIABLE AS IT IS FETCHING FROM
CPU CATCH, THEREFORE, TO SURPASS THIS, WE USE VOLATILE AS IT WILL FORCE
THE SYSTEM TO FETCH THE TARGET VARIABLE DIRECTLY FROM MAIN MEMORY.
*/
private static volatile boolean x = false;
private static Thread thread(){
final Runnable runnable = new Runnable(){
@Override
public void run() {
/*
waits until X = TRUE then finishes
*/
while(!x);
System.out.println("DONE! " + Thread.currentThread().getName());
}
};
return new Thread(runnable);
}
public static void main(String[] args) throws Exception{
thread().start();
thread().start();
thread().start();
Thread.sleep(1000);
x = true;
System.out.println("CHANGED! " + Thread.currentThread().getName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment