public class Apple { public int a = 0; public Apple() { for (int i = 0; i < 50000; i++) {
a = i;
}
}
}public class SimpleCounter {
Apple apple;// volatile Apple apple;
public void setApple() {
apple = new Apple();
} public Apple getApple() { while (true) { if (null != apple) { return apple;
}
}
}
}@RunWith(ConcurrentTestRunner.class)public class TestSimpleCounter { private SimpleCounter simpleCounter = new SimpleCounter(); @Test
public void testSetApple() {
simpleCounter.setApple();
} @Test
public void testGetApple() {
System.out.println(simpleCounter.getApple().a);
}
}为什么上述代码不能停止运行,改成volatile就可以,测试框架用的concurrent-junit。
添加回答
举报
0/150
提交
取消