This topic has been archived. It cannot be replied.
-
工作学习 / IT技术讨论 / Java兄弟们,给大家一道在线面试题,看大家要几秒钟。。。(记着:对方正等你做完,给你下一道)2. This class (IncrementImpl) will be used by various threads
concurrently; can you see the inherent flaw(s)? How would you improve it?
public class IncrementImpl {
private static int counter = 0;
public synchronized void increment() {
counter++;
}
public int getCounter() {
return counter;
}
}
-hostler(春泥);
2005-3-10
{357}
(#2170344@0)
-
如果有几个thread排队要increment(), 另一个thread可以get, 但是它得到的值已经不准确~~
-springtime(春雨);
2005-3-10
(#2170370@0)
-
2个错误:
1.在读数据时也要加锁,
2. 对 static变量应该是用类加锁而不是对象加锁.
-zhg519(Jedi);
2005-3-12
{629}
(#2174205@0)