论坛首页 Java企业应用论坛

我也来说说Vector跟ArrayList的区别

浏览 9600 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (10)
作者 正文
   发表时间:2011-02-27   最后修改:2011-02-27
这几天看到论坛首页有一个挺热的帖子Vector和ArrayList的本质区别到底是什么?,正好是自己正在学习的内容,所以也在这发个帖子,献献丑,如有写得不正确的,还希望各位同学斧正

帖子讨论的是面试中常见的Vector,ArrayList以及与其类似的“线程安全类”的问题

里面有几个关键点:
1.线程安全的定义
2.像Vector,HashTable这样几乎在所有方法都添加上"synchronized"能否达到线程安全的效果?(细心点的同学可能还会发现Collections.synchronizedSortedMap,Collections.synchronizedList等是用了同样的手段)
3.Vector到底有没有性能上的问题呢?

天朝人写的东西都没太大的说服力,所以尽量还是引用老外"Doug Lea"的经典《Java Concurrency in Practice》做为论述的依据

关于问题1的解释是这样的

A class is thread-safe if it behaves correctly when accessed from multiple threads, regardless of the scheduling or interleaving of the execution of those threads by the runtime environment, and with no additional synchronization or other coordination on the part of the calling code.

关于问题2呢,首先,需要知道synchronized一个方法到底是一个怎样的操作
解释是这样的:
Java provides a built-in locking mechanism for enforcing atomicity: the synchronized block. (There is also another critical aspect to locking and other synchronization mechanismsvisibilitywhich is covered in Chapter 3.) A synchronized block has two parts: a reference to an object that will serve as the lock, and a block of code to be guarded by that lock. A synchronized method is a shorthand for a synchronized block that spans an entire method body, and whose lock is the object on which the method is being invoked. (Static synchronized methods use the Class object for the lock.)


Every Java object can implicitly act as a lock for purposes of synchronization; these built-in locks are called intrinsic locks or monitor locks. The lock is automatically acquired by the executing thread before entering a synchronized block and automatically released when control exits the synchronized block, whether by the normal control path or by throwing an exception out of the block. The only way to acquire an intrinsic lock is to enter a synchronized block or method guarded by that lock.

也就是说,如果在每个方法前都加上了"synchronized",会起到这样的效果:当一个线程调用任一个该类的方法时,都要先去获取该对象的固有锁"intrinsic lock",然后才进行操作,如果当一个线程正在执行某个方法时,另一个线程也请求进入,那就需要等待前者执行完才能获得锁,然后继续执行

细心的tx想必看出来了,这样会造成严重的效率问题,比如多个线程同时执行get操作,它们根本是不可能相互影响的,但却也需要分别获得锁才能继续执行,单凭这一点就可以为问题3做一个结论了

然后再说说到底能不能达到线程安全的效果呢?
答案也是否定的

看一个例子:
if (!vector.contains(element))
    vector.add(element);

显然,这个操作违背了上文所说的"A class is thread-safe if it behaves correctly when accessed from multiple threads"的效果

然后继续看问题3
The performance cost of synchronization comes from several sources. The visibility guarantees provided by synchronized and volatile may entail using special instructions called memory barriers that can flush or invalidate caches, flush hardware write buffers, and stall execution pipelines. Memory barriers may also have indirect performance consequences because they inhibit other compiler optimizations; most operations cannot be reordered with memory barriers.
从这段话可以看出,synchronized是会影响jvm的优化的,所以问题3的答案也很明确了:
会有性能上的问题

不过对于一些老公司,看到通篇的使用HashTable和Vector,也不必太担忧了
因为:
Modern JVMs can reduce the cost of incidental synchronization by optimizing away locking that can be proven never to contend. If a lock object is accessible only to the current thread, the JVM is permitted to optimize away a lock acquisition because there is no way another thread could synchronize on the same lock.


在这,也顺便为这本经典书打打广告,反正小弟看了是受益匪浅的,有兴趣的tx可以下载看看
   发表时间:2011-02-27   最后修改:2011-02-27
学习了,支持楼主这种专业认真的态度。

但对于下面这句话我有不同理解,如果不对,请指正。
A class is thread-safe if it behaves correctly when accessed from multiple threads

我认为这句话说的是:
当一个class在多线程下访问下,这个class的行为是正确的 就可以说这个class是线程安全的。

重点是对class behaves 的理解,一般我们说 class 的行为是指 class定义的method。
也就是说,如果class定义的method,能在多线程下访问下,正确地执行,就可以说这个class是线程安全的。

还是拿 vector 作为例子,

  • Vector类有add(element)方法,这个method有同步锁,所以这个方法的实现能在多线程下正确执行的。
  • Vector类有contains(element)方法,这个method调用了indexOf()方法, indexOf()方法有同步锁,所以这个方法的实现能在多线程下正确执行的。


按上边thread-safe定义,是可以认为vector 是一个线程安全的class。

主要问题是:一个class的线程安全的,并不能保证把class的行为组合在一起也是线程安全的。
比如你举的例子:

   if (!vector.contains(element))  
        vector.add(element);  


这个例子组合了Vector类的2个行为:contains() 和 add().这是属于Application level 的逻辑, Vector类无法预计它提供的method(行为)会被怎样组合使用。
Vector类只能保证它的单个method(行为)是线程安全的,它不能保证组合它的多个method(行为)也是线程安全的。

所以,从多线程保护来说,Vector class虽然是一个线程安全的class,但是它的提供的多线程保护的效果其实不大。





3 请登录后投票
   发表时间:2011-02-27  
Vectory是同步的
ArrayList效率高.但不是线程安全的
一般的建议优先使用ArrayList
0 请登录后投票
   发表时间:2011-02-27   最后修改:2011-02-27
哎呀~~~~~~~受益匪浅呐
0 请登录后投票
   发表时间:2011-02-28  
谈线程安全,得先搞清楚线程安全
线程安全有几个等级:
1,不可变.如String
2,无条件线程安全,指内部的同步机制完整地提供了处理并发的功能,如ConcurrentHashMap
3,有条件线程安全,指内部有同步机制,但是不足以提供完整的处理并发的功能,如Vector
4,线程不安全,指内部没有同步机制,必须对每一个方法调用,以及每一组关联方法调用都进行同步,才能处理并发,如ArrayList
5,线程敌意,外部同步也无法达成线程安全,如System.runFinalizersOnExit
2 请登录后投票
   发表时间:2011-02-28  
topolog 写道
学习了,支持楼主这种专业认真的态度。

但对于下面这句话我有不同理解,如果不对,请指正。
A class is thread-safe if it behaves correctly when accessed from multiple threads

我认为这句话说的是:
当一个class在多线程下访问下,这个class的行为是正确的 就可以说这个class是线程安全的。

重点是对class behaves 的理解,一般我们说 class 的行为是指 class定义的method。
也就是说,如果class定义的method,能在多线程下访问下,正确地执行,就可以说这个class是线程安全的。

还是拿 vector 作为例子,

  • Vector类有add(element)方法,这个method有同步锁,所以这个方法的实现能在多线程下正确执行的。
  • Vector类有contains(element)方法,这个method调用了indexOf()方法, indexOf()方法有同步锁,所以这个方法的实现能在多线程下正确执行的。


按上边thread-safe定义,是可以认为vector 是一个线程安全的class。

主要问题是:一个class的线程安全的,并不能保证把class的行为组合在一起也是线程安全的。
比如你举的例子:

   if (!vector.contains(element))  
        vector.add(element);  


这个例子组合了Vector类的2个行为:contains() 和 add().这是属于Application level 的逻辑, Vector类无法预计它提供的method(行为)会被怎样组合使用。
Vector类只能保证它的单个method(行为)是线程安全的,它不能保证组合它的多个method(行为)也是线程安全的。

所以,从多线程保护来说,Vector class虽然是一个线程安全的class,但是它的提供的多线程保护的效果其实不大。



呵呵,你说的是对的,因为个人主观不太喜欢Vector的滥用现象(现在所在的公司就是如此),所以这里用了点偷换概念的技巧
谢谢指出了,不过还是不打算修改帖子的内容了,呵呵
0 请登录后投票
   发表时间:2011-02-28   最后修改:2011-02-28

通过这个帖子的学习,我明白了一下几点:
1.线程安全的定义,线程安全的类只能保证自身method内部数据在多线程情况下安全,而不保证自身method被其它类组合调用时安全,多数情况下,vector的同步意义并没有起多大作用,反而降低效率。
2.synchronized关键字的真实含义,访问synchronized方法会获取对象的锁,这时可以同时访问没有synchronized关键字的method,而再访问下一个synchronized方法时,会等待对象释放锁。如果数据是不可变的(没有set),不必同步,一旦有变化,就必须对set和get都实施synchronized关键字。
3.线程安全的等级

一个没有看过《Java Concurrency in Practice》并发菜鸟飘过~

0 请登录后投票
   发表时间:2011-02-28  
对"线程敌意"很好奇,使用场景是什么?

gtssgtss 写道
谈线程安全,得先搞清楚线程安全
线程安全有几个等级:
1,不可变.如String
2,无条件线程安全,指内部的同步机制完整地提供了处理并发的功能,如ConcurrentHashMap
3,有条件线程安全,指内部有同步机制,但是不足以提供完整的处理并发的功能,如Vector
4,线程不安全,指内部没有同步机制,必须对每一个方法调用,以及每一组关联方法调用都进行同步,才能处理并发,如ArrayList
5,线程敌意,外部同步也无法达成线程安全,如System.runFinalizersOnExit

0 请登录后投票
   发表时间:2011-02-28  
自己写的业务肯定要自己保证他的正确性啊。

vector是线程安全的,他的方法是同步的,但如果你自己不保证自己的逻辑是线程安全的,那有什么办法。毕竟那些写api的人没法预测你的所有的业务逻辑。

譬如说concurrectHashMap.putIfAbsent(key, value)方法, 其实他也是加了锁的

V put(K key, int hash, V value, boolean onlyIfAbsent) {
            lock();
........................
} finally {
       unlock();
}

其实很多时候明白怎么样运作的是很重要的,而不是一味的用傻瓜方法。

楼主对A class is thread-safe if it behaves correctly when accessed from multiple threads的解释非常误导人。
0 请登录后投票
   发表时间:2011-02-28  
tonynju 写道
对"线程敌意"很好奇,使用场景是什么?

gtssgtss 写道
谈线程安全,得先搞清楚线程安全
线程安全有几个等级:
1,不可变.如String
2,无条件线程安全,指内部的同步机制完整地提供了处理并发的功能,如ConcurrentHashMap
3,有条件线程安全,指内部有同步机制,但是不足以提供完整的处理并发的功能,如Vector
4,线程不安全,指内部没有同步机制,必须对每一个方法调用,以及每一组关联方法调用都进行同步,才能处理并发,如ArrayList
5,线程敌意,外部同步也无法达成线程安全,如System.runFinalizersOnExit



没什么使用场景,这不是什么好东西,出现的原因主要是static字段处理的不好
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics