c**e 发帖数: 1 | 1 One class has a List- , and exposes it as an unmodifiable list:
public List- getItems() {
syncrhonized (this.items) {
return Collections.unmodifiableList(this.items):
}
}
this.items may be accessed by multiple threads. It is possible new item may
be added or removed while the caller is reading through the list obtained
from getItems(). But what the caller needs is just a best-effort snapshot
of the items, and missing a few new items is ok for the caller. Does the
above method work? In other words, can the caller safely navigate through
the snapshot data without any data corruption?
Maybe a fully thread-safe approach is just to return a new list:
public List- getItems() {
synchronized(this.items) {
return new ArrayList- (this.items);
}
}
是不是太浪费了?
碰到这种问题,大家都是怎样处理的? 谢谢。 | f*******n 发帖数: 12623 | | z****e 发帖数: 54598 | | t*******e 发帖数: 684 | 4 那个synchronized一点用都没有。还是web programming好啊,concurrency都扔给数据
库处理了。 |
|