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. 手撕面试官系列(四 ):MongoDB+Redis 面试专题

    MongoDB   (面试题+答案领取方式见侧边栏) 你说的 NoSQL 数据库是什么意思?NoSQL 与 RDBMS 直接有什么区别?为什么要使用和不使用NoSQL 数据库?说一说 NoSQL 数据 ...

  2. 一秒可生成500万ID的分布式自增ID算法—雪花算法 (Snowflake,Delphi 版)

    概述 分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的. 有些时候我们希望能使用一种 ...

  3. RHEL6.5 移植使用CentOS 的YUM 步骤

    问题:使用 Red Hat Enterprise Linux Server(RHEL) yum安装软件时显示 This system is not registered with RHN. RHN s ...

  4. JAVA十六进制数据接收与传输

    一.十六进制转换工具类 主要包含十六进制字符串转ASCII,ASCII转十六进制字符串以及十六进制字符串转Byte数组等方法: /** * Created by wly on 2018/4/17. * ...

  5. C#月份和日期转大写和C#集合分组

    //日转化为大写 private static string DaytoUpper(int day, string type) { if (day < 20) { return MonthtoU ...

  6. java之spring之整合ssh-2

    这篇也是主要讲解 ssh 的整合,不同于上一篇的是它的注入方式. 这篇会采用扫描注入的方式,即去除 applicationContext-asd.xml 文件. 目录结构如下: 注意,这里只列举不同的 ...

  7. 单点logi,n

    using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; u ...

  8. Deployment.spec.selector.matchLables实验解释

    原文:https://cloud.tencent.com/developer/article/1394657 Deployment.spec.selector.matchLables实验解释 作者: ...

  9. 易百教程人工智能python修正-人工智能NLTK性别发现器

    在这个问题陈述中,将通过提供名字来训练分类器以找到性别(男性或女性). 我们需要使用启发式构造特征向量并训练分类器.这里使用scikit-learn软件包中的标签数据. 以下是构建性别查找器的Pyth ...

  10. 79.mobile/js---手机端分享调用功能的实现(只能在真机上测试有效)

    html:<a href="javascript:void(0);" class="all-share" onclick="call('defa ...