没有对Collections.synchronizedList研究过,下面是javadoc里面的描述 synchronizedList public static List synchronizedList(List list)Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list. It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } Failure to follow this advice may result in non-deterministic behavior. The returned list will be serializable if the specified list is serializable. Parameters: list - the list to be "wrapped" in a synchronized list. Returns: a synchronized view of the specified list.
ArrayList不是
很明显,Vector的开销大
synchronizedList
public static List synchronizedList(List list)Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that all access to the backing list is accomplished through the returned list.
It is imperative that the user manually synchronize on the returned list when iterating over it: List list = Collections.synchronizedList(new ArrayList());
...
synchronized(list) {
Iterator i = list.iterator(); // Must be in synchronized block
while (i.hasNext())
foo(i.next());
}
Failure to follow this advice may result in non-deterministic behavior.
The returned list will be serializable if the specified list is serializable.
Parameters:
list - the list to be "wrapped" in a synchronized list.
Returns:
a synchronized view of the specified list.
但是这个类必须放在synchronized块里面处理,从而保证同步而Vector是一个古老的容器,他本身是线程 安全的,但是是以牺牲性能的代价获得的如果说比较性能,我觉得是其区别就是对同步的实现区别而已
Vector类里面的方法都是synchronized从而实现了同步
而使用Collections.synchronizedList,是需要用synchronized块来实现的
两者的同步的对象是不一样的,性能上我觉得不可比较
synchronized(mutex)
比synchronized(this)效率高一点