由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Java版 - Unmodifiable List concurrent access 问题
相关主题
List, LinkedList and Vector问个 Generic 的问题
immutable list发现 synchronized 的一个问题
state::update的用法是Java 8吗 (转载)synchronization for counters
请问这个面试题,关于synchronize hashmapsynchronization 锁住了什么?
Generic type cast warning新手问个multi-threading关于synchronized和volatile的问题
ArrayList and Link listweb service returns HashMap that contains multiple ArrayList
is access to int[] faster than List?Talk a little more about How to lock a file
ArraList question请教一个多线程lock机制的问题
相关话题的讨论汇总
话题: list话题: item话题: getitems话题: caller
进入Java版参与讨论
1 (共1页)
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
2
No, it is not safe.
z****e
发帖数: 54598
3
copyonwritearraylist
t*******e
发帖数: 684
4
那个synchronized一点用都没有。还是web programming好啊,concurrency都扔给数据
库处理了。
1 (共1页)
进入Java版参与讨论
相关主题
请教一个多线程lock机制的问题Generic type cast warning
怎么synchronize时间捏ArrayList and Link list
HashMap cacheis access to int[] faster than List?
Apply lock on a class.ArraList question
List, LinkedList and Vector问个 Generic 的问题
immutable list发现 synchronized 的一个问题
state::update的用法是Java 8吗 (转载)synchronization for counters
请问这个面试题,关于synchronize hashmapsynchronization 锁住了什么?
相关话题的讨论汇总
话题: list话题: item话题: getitems话题: caller