差不多一样,为什么说Vector性能开销小?
前者是新的使用方式,后者是古老的使用方式

解决方案 »

  1.   

    Vector 是synchronized,
    ArrayList不是
    很明显,Vector的开销大
      

  2.   

    他们的区别是什么呢?注意我这里说的并不是ArrayList哦,看清楚哦
      

  3.   

    没有对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.
      

  4.   

    那我的理解就是Collections.synchronizedList得到的其实只是一个普通的List
    但是这个类必须放在synchronized块里面处理,从而保证同步而Vector是一个古老的容器,他本身是线程 安全的,但是是以牺牲性能的代价获得的如果说比较性能,我觉得是其区别就是对同步的实现区别而已
    Vector类里面的方法都是synchronized从而实现了同步
    而使用Collections.synchronizedList,是需要用synchronized块来实现的
    两者的同步的对象是不一样的,性能上我觉得不可比较
      

  5.   

    刚才仔细看了synchronizedList的源码,刚才的理解可能有点不正确synchronizedList是synchronizedCollection的子类,她的实现是用synchronized块通过信号量来实现同步的,其实和Vector的实现还是不一样的
      

  6.   

    这应该是一点点地优化吧
    synchronized(mutex)
    比synchronized(this)效率高一点