Collections.synchronizedList与CopyOnWriteArrayList比较
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比较的更多相关文章
- Collections.synchronizedList 、CopyOnWriteArrayList、Vector介绍、源码浅析与性能对比
## ArrayList线程安全问题 众所周知,`ArrayList`不是线程安全的,在并发场景使用`ArrayList`可能会导致add内容为null,迭代时并发修改list内容抛`Concurre ...
- CopyOnWriteArrayList与Collections.synchronizedList的性能对比
列表实现有ArrayList.Vector.CopyOnWriteArrayList.Collections.synchronizedList(list)四种方式. 1 ArrayList Array ...
- CopyOnWriteArrayList与Collections.synchronizedList的性能对比(转)
列表实现有ArrayList.Vector.CopyOnWriteArrayList.Collections.synchronizedList(list)四种方式. 1 ArrayList Array ...
- CopyOnWriteArrayList&Collections.synchronizedList()
1.ArrayList ArrayList是非线性安全,此类的 iterator() 和 listIterator() 方法返回的迭代器是快速失败的:在创建迭代器之后,除非通过迭代器自身的 remov ...
- ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 以及 同步的集合类 Hashtable 和 Vector Collections.synchronizedMap 和 Collections.synchronizedList 区别缺点
ConcurrentHashMap和 CopyOnWriteArrayList提供线程安全性和可伸缩性 DougLea的 util.concurrent 包除了包含许多其他有用的并发构造块之外,还包含 ...
- Collections.synchronizedList线程安全性陷阱
摘要: 详细的解析:Collections.synchronizedList 1 :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedLi ...
- 【集合类型的并发】Collections.synchronizedList
摘要: 详细的解析:Collections.synchronizedList :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedList ...
- Collections.synchronizedList使用
1.SynchronizedList类具体代码: static class SynchronizedList<E> extends SynchronizedCollection<E& ...
- 线程安全Collections.synchronizedList
ollections.synchronizedList引发的线程安全问题 有些容器是线程安全的(Vector,ConcurrentLinkedQueue等),有些则不是(list等),利用类 似 pr ...
随机推荐
- Versioning information could not be retrieved from the NuGet package repository. Please try again later.
Versioning information could not be retrieved from the NuGet package repository. Please try again la ...
- 0-python变量及基本数据类型
目录 1.变量2.字符串3.布尔类型4.整数5.浮点数6.日期 1.变量 1.1.变量的定义 - 类似于标签 1.2.变量的命名规则 - (强制)变量名只能包含数字.字母.下划线 - (强制)不能以数 ...
- 进程池和线程池、协程、TCP单线程实现并发
一.进程池和线程池 当被操作对象数目不大时,我们可以手动创建几个进程和线程,十几个几十个还好,但是如果有上百个上千个.手动操作麻烦而且电脑硬件跟不上,可以会崩溃,此时进程池.线程池的功效就能发挥了.我 ...
- C 猜猜猜😀文字小游戏
前言 - 随机性 随机数生成 - https://zh.wikipedia.org/wiki/%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%94%9F%E6%88%90 没啥事情, 写 ...
- Golang 读写文件
读文件 func ReadFile_v1(filename string) { var ( err error content []byte ) fileObj,err := os.Open(file ...
- ubuntu supervisor管理uwsgi+nginx
一.概述 superviosr是一个Linux/Unix系统上的进程监控工具,他/她upervisor是一个Python开发的通用的进程管理程序,可以管理和监控Linux上面的进程,能将一个普通的命令 ...
- linux初学者-编辑文件工具vim
"vim"是linux中非常强大,应用非常广的编辑方式.下面介绍一些"vim"的基本用法.以"/etc/passwd"为例. 1.vim ...
- mysql 8.0.17 安装与使用
目录 写在前面 MySQL 安装 重置密码 使用图形界面软件 Navicat for SQL 写在前面 以前包括现在接到的项目,用的最多的关系型数据库就是SqlServer或者Oracle.后来因为接 ...
- 在部署 C#项目时转换 App.config 配置文件
问题 部署项目时,常常需要根据不同的环境使用不同的配置文件.例如,在部署网站时可能希望禁用调试选项,并更改连接字符串以使其指向不同的数据库.在创建 Web 项目时,Visual Studio 自动生成 ...
- 用lua求两个数组的交集、并集和补集。
-- 克隆 function Clone(object) local lookup_table = { } local function _copy(object) if type(object) ~ ...