1.单线程方式

2.多线程版本,不安全的 ArrayList

3.多线程版本,线程安全,CopyOnWriteArrayList()方式

4.多线程版本,线程安全,Collections.synchronizedList方式

 import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; /**
* @author zsh
* @site qqzsh.top
* @create 2019-08-26 11:53
* @description Collections.synchronizedList与CopyOnWriteArrayList比较
* https://liuyanzhao.com/9732.html
*/
public class Main3 { /**
* 单线程:性能较差
*/
static void f1(){
Long startTime = System.currentTimeMillis();
//这是一个长度为1000的集合
List<Long> sourceList = new ArrayList<>();
for (long i = 0L; i < 1000L; i++) {
sourceList.add(i);
}
System.out.println("原列表大小:" + sourceList.size());
//对原列表进行处理
List<Long> resultList = new ArrayList<>();
for (Long x : sourceList) {
//模拟耗时操作(x累加300万次)
Long sum = 0L;
for (long i = 0L; i < 3000000L; i++) {
sum += x;
}
resultList.add(sum);
}
System.out.println("处理后的列表大小:" + resultList.size());
System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
} /**
* 多线程版本,不安全的 ArrayList
*/
static void f2(){
Long startTime = System.currentTimeMillis();
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
//这是一个长度为1000的集合
List<Long> sourceList = new ArrayList<>();
for (long i = 0L; i < 1000L; i++) {
sourceList.add(i);
}
System.out.println("原列表大小:" + sourceList.size());
List<Long> resultList = new ArrayList<>();
for (Long x : sourceList) {
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
//对原列表进行处理
//模拟耗时操作(x累加300万次)
Long sum = 0L;
for (long i = 0L; i < 3000000L; i++) {
sum += x;
}
resultList.add(sum);
}
});
}
fixedThreadPool.shutdown();//关闭线程池
//此处不可以删除或注释,需要线程执行结束后再执行别的内容,即只有线程结束后才会继续向下执行
while (!fixedThreadPool.isTerminated()) {
}
System.out.println("处理后的列表大小:" + resultList.size());
System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
} /**
* 多线程版本,线程安全,CopyOnWriteArrayList()方式
*/
static void f3(){
Long startTime = System.currentTimeMillis();
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
//这是一个长度为1000的集合
List<Long> sourceList = new ArrayList<>();
for (long i = 0L; i < 1000L; i++) {
sourceList.add(i);
}
System.out.println("原列表大小:" + sourceList.size());
List<Long> resultList = new CopyOnWriteArrayList<>();
for (Long x : sourceList) {
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
//对原列表进行处理
//模拟耗时操作(x累加300万次)
Long sum = 0L;
for (long i = 0L; i < 3000000L; i++) {
sum += x;
}
resultList.add(sum);
}
});
}
fixedThreadPool.shutdown();//关闭线程池
//此处不可以删除或注释,需要线程执行结束后再执行别的内容,即只有线程结束后才会继续向下执行
while (!fixedThreadPool.isTerminated()) {
}
System.out.println("处理后的列表大小:" + resultList.size());
System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
} /**
* 多线程版本,线程安全,Collections.synchronizedList方式
*/
static void f4(){
Long startTime = System.currentTimeMillis();
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10);
//这是一个长度为1000的集合
List<Long> sourceList = new ArrayList<>();
for (long i = 0L; i < 1000L; i++) {
sourceList.add(i);
}
System.out.println("原列表大小:" + sourceList.size());
List<Long> resultList = Collections.synchronizedList(new ArrayList<>());
for (Long x : sourceList) {
fixedThreadPool.execute(new Runnable() {
@Override
public void run() {
//对原列表进行处理
//模拟耗时操作(x累加300万次)
Long sum = 0L;
for (long i = 0L; i < 3000000L; i++) {
sum += x;
}
resultList.add(sum);
}
});
}
fixedThreadPool.shutdown();//关闭线程池
//此处不可以删除或注释,需要线程执行结束后再执行别的内容,即只有线程结束后才会继续向下执行
while (!fixedThreadPool.isTerminated()) {
}
System.out.println("处理后的列表大小:" + resultList.size());
System.out.println("耗时:" + (System.currentTimeMillis() - startTime) + "ms");
} public static void main(String[] args) {
/*f1();*/
/*f2();*/
/*f3();*/
f4();
}
}

Collections.synchronizedList与CopyOnWriteArrayList比较的更多相关文章

  1. Collections.synchronizedList 、CopyOnWriteArrayList、Vector介绍、源码浅析与性能对比

    ## ArrayList线程安全问题 众所周知,`ArrayList`不是线程安全的,在并发场景使用`ArrayList`可能会导致add内容为null,迭代时并发修改list内容抛`Concurre ...

  2. CopyOnWriteArrayList与Collections.synchronizedList的性能对比

    列表实现有ArrayList.Vector.CopyOnWriteArrayList.Collections.synchronizedList(list)四种方式. 1 ArrayList Array ...

  3. CopyOnWriteArrayList与Collections.synchronizedList的性能对比(转)

    列表实现有ArrayList.Vector.CopyOnWriteArrayList.Collections.synchronizedList(list)四种方式. 1 ArrayList Array ...

  4. CopyOnWriteArrayList&Collections.synchronizedList()

    1.ArrayList ArrayList是非线性安全,此类的 iterator() 和 listIterator() 方法返回的迭代器是快速失败的:在创建迭代器之后,除非通过迭代器自身的 remov ...

  5. ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 以及 同步的集合类 Hashtable 和 Vector Collections.synchronizedMap 和 Collections.synchronizedList 区别缺点

    ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含 ...

  6. Collections.synchronizedList线程安全性陷阱

    摘要: 详细的解析:Collections.synchronizedList 1 :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedLi ...

  7. 【集合类型的并发】Collections.synchronizedList

    摘要: 详细的解析:Collections.synchronizedList :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedList ...

  8. Collections.synchronizedList使用

    1.SynchronizedList类具体代码: static class SynchronizedList<E> extends SynchronizedCollection<E& ...

  9. 线程安全Collections.synchronizedList

    ollections.synchronizedList引发的线程安全问题 有些容器是线程安全的(Vector,ConcurrentLinkedQueue等),有些则不是(list等),利用类 似 pr ...

随机推荐

  1. redis学习(一)

    Redis学习内容: 1. 概念 2. 下载安装R 3. 命令操作 1. 数据结构 4. 持久化操作 5. 使用Java客户端操作redis 1. 概念: redis是一款高性能的NOSQL系列的非关 ...

  2. 如何理解 PHP的依赖注入(DI) 和 控制反转(IoC)

    名词解释: IoC - Inversion of Control 控制反转 DI - Dependency Injection 依赖注入 依赖注入和控制反转说的实际上是同一个东西,它们是一种设计模式, ...

  3. DELPHI6中DSGNINTF.DCU找不到时的解决方法

    https://www.cnblogs.com/gaodu2003/archive/2009/06/04/1495789.html 1.添加 lib\designide.dcp到控件的dpk文件的re ...

  4. day08——文件操作

    day08 文件操作: open() :打开 f (文件句柄)= open("文件的路径(文件放的位置)",mode="操作文件的模式",encoding=&q ...

  5. LeetCode 1253. 重构 2 行二进制矩阵 - Java - 统计

    题目链接:https://leetcode-cn.com/contest/weekly-contest-162/problems/reconstruct-a-2-row-binary-matrix/ ...

  6. 手把手教你做JavaWeb项目:登录模块

    现如今,无论是客户端还是移动端,无论是游戏登陆还是社交平台登陆,无处不在的“登陆”.那么你知道怎么制作吗?今天就为你娓娓道来: 用户登录 在各大信息管理系统中,登录功能是必不可少的,他的作用就是验证用 ...

  7. unity---为什么用Time.deltaTime * speed 表示每秒移动的距离的理解

    Time.deltaTime:代表时间增量,即从上一帧到当前帧消耗的时间, 这个值是动态变化的. dt 表示 deltaTime. 假如 1s渲染10帧,沿X轴方向的移动速度 speed = 10m/ ...

  8. Oracle打印输出在控制台

    SET SERVEROUTPUT ON  --必须有,不然显示不出declare LN_C number(10,0):=0;begin DECLARE LS_STR1 VARCHAR2(200); - ...

  9. .NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖

    原文:.NET 使用 ILMerge 合并多个程序集,避免引入额外的依赖 我们有多种工具可以将程序集合并成为一个.打包成一个程序集可以避免分发程序的时候带上一堆依赖而出问题. ILMerge 可以用来 ...

  10. Raft选举算法

    目标:分布式集群中,选举Leader,保持数据一致性   集群中每个节点都有三种状态: Follower:纯小弟 Candidate:候选人.我原来是小弟,但我现在想当老大 Leader:老大 集群状 ...