- public class Test {
- public static void main(String[] args) {
- final T t = new T();
- new Thread() {
- @Override
- public void run() {
- t.inc();
- }
- }.start();
- new Thread() {
- @Override
- public void run() {
- t.get();
- }
- }.start();
- }
- static class T {
- private volatile int a;
- private volatile int b;
- public void get() {
- while (a >= b) {}
- System.out.println("a(" + a + "), b(" + b + ")");
- System.exit(0);
- }
- public synchronized void inc() {
- while (true) {
- a++;
- b++;
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/1912201411353.html
来源: http://www.codesnippet.cn/detail/1912201411353.html