Skip to content

Instantly share code, notes, and snippets.

@lihongjie0209
Created December 5, 2018 00:51
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 lihongjie0209/26c966b808c03075427d7c72ff34a707 to your computer and use it in GitHub Desktop.
Save lihongjie0209/26c966b808c03075427d7c72ff34a707 to your computer and use it in GitHub Desktop.
public class CounterForVisibility extends Thread {
private volatile static int count = 0;
public static synchronized int getCount() {
return count;
}
public synchronized void setCount(int count) {
this.count = count;
}
public static void main(String[] args) throws InterruptedException {
CounterForVisibility counter = new CounterForVisibility();
counter.start();
Thread.sleep(100);
System.out.println("把计数器设置为1");
counter.setCount(1);
}
@Override
public void run() {
while (Counter.getCount() == 0) {
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment