public class NamedCounter {
private final String name;
private int count; public NamedCounter(String name) { this.name = name; }
public String getName() { return name; }
public void increment() { count++; }
public int getCount() { return count; }
public void reset() { count = 0; }
}
Which three changes should be made to adapt this class to be used safely by multiple threads?
(Choose three.)
A. declare reset() using the synchronized keyword
B. declare getName() using the synchronized keyword
C. declare getCount() using the synchronized Keyword
D. declare the constructor using the synchronized keyword
E. declare increment() using the synchronized keyword
Answer: ACE 我觉得唯有E是正确的
private final String name;
private int count; public NamedCounter(String name) { this.name = name; }
public String getName() { return name; }
public void increment() { count++; }
public int getCount() { return count; }
public void reset() { count = 0; }
}
Which three changes should be made to adapt this class to be used safely by multiple threads?
(Choose three.)
A. declare reset() using the synchronized keyword
B. declare getName() using the synchronized keyword
C. declare getCount() using the synchronized Keyword
D. declare the constructor using the synchronized keyword
E. declare increment() using the synchronized keyword
Answer: ACE 我觉得唯有E是正确的
要说i++这种,我记得执行时候是分三步的,但直接赋个数值不会吧。
Long和double以外的基本类型的读写操作都是原子操作,应该是不需要同步的,所以不知道为什么选A
好像有点矛盾 是我理解错了
counts 是 int的是基本类型 return counts属于基本操作 但是它是不安全的 和下面的 应该是不需要同步的 好像有点矛盾
关于 不稳定的中间状态,存在可视性听过不太懂
可是return counts是不安全的,原因我在5楼回复了
哦 明白了
能不能在解释一下原子操作中 除了 return 语句外 还有那些是不安全的
http://blog.csdn.net/mabaolin/archive/2007/06/15/1653178.aspx