Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Created July 12, 2014 12:41
Show Gist options
  • Select an option

  • Save adohe-zz/e73e0530312a3e0e0023 to your computer and use it in GitHub Desktop.

Select an option

Save adohe-zz/e73e0530312a3e0e0023 to your computer and use it in GitHub Desktop.
Memory visibility when lack of synchronization
package com.ado.java;
public class NoVisibility {
private static int number;
private static boolean ready;
private static class ReaderThread extends Thread {
@Override
public void run() {
while (!ready)
Thread.yield();
System.out.println(number);
}
}
public static void main(String[] args) {
new ReaderThread().start();
number = 42;
ready = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment