一些列表类及其特性

 类 线程安全 Iterator 特性 说明
Vector fail-fast 内部方法用synchronized修饰,因此执行效率较低

1. 线程安全的列表类并不意味着调用它的代码就一定线程安全

2. 只有CopyOnWriteArrayList能支持在遍历时修改列表元素

ArrayList fail-fast 在多线程环境中使用不当易出错
Collections.synchronizedList fail-fast Collections.synchronizedList可以得到线程安全的列表,与Vector类似
CopyOnWriteArrayList fail-safe 每个线程先复制一份并把地址指向新list,在新的list上操作,因此最终结果未必符合预期,适用于经常需要读但很少修改的场景

以下代码模拟多线程环境下,各个类Iterator机制的表现

package com.concurrent.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch; /**
* 多线程操作列表
*/
public class ThreadListTest { public void multipleThreadArrayList(final List<Integer> list) throws InterruptedException {
int count = 10;
for (int i = 0; i < count; i++) {
list.add(i);
} final CountDownLatch countDownLatch = new CountDownLatch(2);
new Thread(new Runnable() { @Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
e1.printStackTrace();
} for (Integer integer : list) {
System.out.println(integer);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
countDownLatch.countDown();
}
},"foreach-Thread").start(); new Thread(new Runnable() { @Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadId:" + Thread.currentThread().getId() + " remove " + list.remove(0));
countDownLatch.countDown();
}
},"remove-Thread").start(); countDownLatch.await();
} public static void main(String[] args) throws InterruptedException {
ThreadListTest test = new ThreadListTest();
List<Integer> list ; list = new Vector<>();// Vector只保证内部方法线程安全
list = new ArrayList<>();// ArrayList效率比Vector高,但非线程安全
list = Collections.synchronizedList(new ArrayList<Integer>());// Collections.synchronizedList与Vector异曲同工
list = new CopyOnWriteArrayList<>();// CopyOnWriteArrayList线程安全:每个线程先复制一份并把地址指向新list,在新的list上操作,因此最终结果未必符合预期 test.multipleThreadArrayList(list);
System.out.println(list.toString()); } }

【Java多线程系列五】列表类的更多相关文章

  1. Java多线程系列五——列表类

    参考资料: http://xxgblog.com/2016/04/02/traverse-list-thread-safe/ 一些列表类及其特性  类 线程安全 Iterator 特性 说明 Vect ...

  2. java多线程系列(五)---synchronized ReentrantLock volatile Atomic 原理分析

    java多线程系列(五)---synchronized ReentrantLock volatile Atomic 原理分析 前言:如有不正确的地方,还望指正. 目录 认识cpu.核心与线程 java ...

  3. java多线程系列五、并发容器

    一.ConcurrentHashMap 1.为什么要使用ConcurrentHashMap 在多线程环境下,使用HashMap进行put操作会引起死循环,导致CPU利用率接近100%,HashMap在 ...

  4. Java多线程系列九——Atomic类

    参考资料:https://fangjian0423.github.io/2016/03/16/java-AtomicInteger-analysis/http://www.cnblogs.com/54 ...

  5. Java多线程系列二——Thread类的方法

    Thread实现Runnable接口并实现了大量实用的方法 public static native void yield(); 此方法释放CPU,但并不释放已获得的锁,其它就绪的线程将可能得到执行机 ...

  6. (Java多线程系列五)守护线程

    守护线程 什么是守护线程 Java中有两种线程,一种是用户线程,一种是守护线程. 当进程不存在或主线程停止,守护线程也会自动停止. class DaemonThread extends Thread ...

  7. Java多线程系列--“JUC锁”10之 CyclicBarrier原理和示例

    概要 本章介绍JUC包中的CyclicBarrier锁.内容包括:CyclicBarrier简介CyclicBarrier数据结构CyclicBarrier源码分析(基于JDK1.7.0_40)Cyc ...

  8. Java多线程系列--“JUC锁”02之 互斥锁ReentrantLock

    本章对ReentrantLock包进行基本介绍,这一章主要对ReentrantLock进行概括性的介绍,内容包括:ReentrantLock介绍ReentrantLock函数列表ReentrantLo ...

  9. Java多线程系列--“JUC锁”09之 CountDownLatch原理和示例

    概要 前面对"独占锁"和"共享锁"有了个大致的了解:本章,我们对CountDownLatch进行学习.和ReadWriteLock.ReadLock一样,Cou ...

随机推荐

  1. vim 更改注释颜色

    在 ~/.vimrc 添加命令: highlight Comment ctermfg=green

  2. Hexo next 添加复制粘贴代码的功能

    文章目录 广告: 自己的方式 感谢 广告: 本人博客地址:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.github.io ...

  3. ABTest介绍及abtest流量切换实现

    本文为学习abtest切流方案方便以后查看大部分内容转载自原文 https://blog.csdn.net/tanweii163/article/details/80543083 互联网公司的业务发展 ...

  4. 在命令行中插入TAB键

    参考man bash: quoted-insert (C-q, C-v) Add the next character typed to the line verbatim.  This is how ...

  5. pytest_参数化3

    import pytesttest_user_data=[ {'user':'linda','password':'8888'}, {'user':'servenruby','password':'1 ...

  6. bzoj [POI2015]Myjnie

    [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special Judge Description 有n家洗车店从左往右排成一排, ...

  7. C语言printf函数

    #include<stdio.h> //int float double short char long int main() { //int printf(const char *for ...

  8. slim.arg_scope()的使用

    [https://blog.csdn.net/u013921430 转载] slim是一种轻量级的tensorflow库,可以使模型的构建,训练,测试都变得更加简单.在slim库中对很多常用的函数进行 ...

  9. AtCoder Grand Contest 001F Wide Swap

    解法参考这位大佬的:https://www.cnblogs.com/BearChild/p/7895719.html 因为原来的数组不好做于是我们想反过来数组,根据交换条件:值相邻且位置差大于等于k, ...

  10. day08 python文件操作

    day08 python   一.文件操作     1.文件操作的函数         open(文件名, mode=模式, encoding=字符集)       2.模式: r, w, a, r+ ...