线程安全的集合有哪些
- IT科技
- 关注:2.45W次
品牌型号:联想 YOGA 14c/系统版本:windows7
线程安全的集合有Vector、HashTable、Stack、arrayBlockingQueue、ConcurrentHashMap、ConcurrentLinkedQueue等。
Vector相当于 ArrayList 的翻版,是长度可变的数组,Vector的每个方法都加了 synchronized 修饰符,是线程安全的。
Hashtable是一个线程安全的集合,是单线程集合,它给几乎所有public方法都加上了synchronized关键字。
Stack继承于Vector, 栈是后进先出的。
ArrayBlockingQueue是一个阻塞队列,底层使用数组结构实现,按照先进先出(FIFO)的原则对元素进行排序。
ConcurrentHashMap 采用了分段锁(Segment),HashTable的加锁方法是给每个方法加上synchronized关键字,线程安全。
ConcurrentLinkedQueue是一种FIFO的无界队列,是线程安全的,它适用于“高并发”的场景。
标签:
线程
- 文章版权属于文章作者所有,转载请注明 https://shqsg.com/zh-sg/dianzi/r5yz1x.html