标签:同步 min int max sum List System new 比较
1,引言
java线程安全的List实现有以下三种:
new Vector<>()
Collections.synchronizedList(new ArrayList<>())
new CopyOnWriteArrayList<>()
通常认为使用了synchronized
会导致运行变慢,那么在java
针对synchronized
进行一系列优化后,现在的情况如何呢?为了检验这一说法,写了一个验证程序进行验证。
2,验证代码
以ArrayList
作为基础,分别测试4种List
的顺序写入(0
~ 1 << 24
)、顺序读取和随机读取,各十轮。据此编写代码如下
import java.util.*;
import java.util.concurrent.*;
public class VectorTest {
private static final int CNT = 1 << 24;
private static final Random rand = new Random();
public static void main(String[] args) {
int writeRound = 10, readRound = 10, randomReadRound = 10;
List<Integer> ls = new ArrayList<>();
test(ls, "ArrayList", writeRound, readRound, randomReadRound);
test(new Vector<>(), "Vector", writeRound, readRound, randomReadRound);
test(Collections.synchronizedList(new ArrayList<>()), "SynArrayList", writeRound, readRound, randomReadRound);
List<Integer> ls2 = new CopyOnWriteArrayList<>();
ls2.addAll(ls);
test(ls2, "COWList", 0, readRound, randomReadRound);
}
private static void test(List<Integer> list, String name, int writeRound, int readRound, int randomReadRound) {
int max = 0, min = Integer.MAX_VALUE, sum = 0;
int[] w = new int[writeRound], r = new int[readRound], rr = new int[randomReadRound];
for (int i = 0; i < writeRound; i++) {
list.clear();
int v = w[i] = writeTest(list);
max = Math.max(max, v);
min = Math.min(min, v);
sum += v;
}
System.out.printf("%s write test: sum = %d, max = %d, min = %d\n", name, sum, max, min);
for (int v : w) System.out.printf("%d\t", v);
System.out.println();
sum = max = 0;
min = Integer.MAX_VALUE;
for (int i = 0; i < readRound; i++) {
int v = r[i] = readTest(list);
max = Math.max(max, v);
min = Math.min(min, v);
sum += v;
}
System.out.printf("%s read test: sum = %d, max = %d, min = %d\n", name, sum, max, min);
for (int v : r) System.out.printf("%d\t", v);
System.out.println();
sum = max = 0;
min = Integer.MAX_VALUE;
for (int i = 0; i < randomReadRound; i++) {
int v = rr[i] = randomReadTest(list);
max = Math.max(max, v);
min = Math.min(min, v);
sum += v;
}
System.out.printf("%s random read test: sum = %d, max = %d, min = %d\n", name, sum, max, min);
for (int v : rr) System.out.printf("%d\t", v);
System.out.println();
}
private static int writeTest(List<Integer> list) {
long t0 = System.currentTimeMillis();
for (int i = 0; i < CNT; i++) list.add(i);
long t1 = System.currentTimeMillis();
return (int)(t1 - t0);
}
private static int readTest(List<Integer> list) {
long t0 = System.currentTimeMillis();
for (int i = 0; i < CNT; i++) list.get(i);
long t1 = System.currentTimeMillis();
return (int)(t1 - t0);
}
private static int randomReadTest(List<Integer> list) {
long t0 = System.currentTimeMillis();
for (int i = 0; i < CNT; i++) list.get(rand.nextInt(CNT));
long t1 = System.currentTimeMillis();
return (int)(t1 - t0);
}
}
3,测试平台
垃圾笔记本,使用Intel酷睿i5 7200U
java版本为java 12,HotSpot虚拟机
4,测试结果
单位:毫秒
5,结果分析
ArrayList
(A)、Vector
(V)、Collections.synchronizedList(new ArrayList<>())
(S)、以及CopyOnWriteArrayList
(C)四种类型的结果分别如下
十轮写入,单位毫秒
A | V | S | C | |
---|---|---|---|---|
总时间 | 5889 | 8219 | 7079 | inf |
最大时间 | 958 | 1137 | 1756 | inf |
最小时间 | 404 | 662 | 539 | inf |
十轮单线程顺序读,单位毫秒
A | V | S | C | |
---|---|---|---|---|
总时间 | 32 | 2079 | 1142 | 645 |
最大时间 | 17 | 396 | 138 | 78 |
最小时间 | 0 | 185 | 102 | 62 |
十轮单线程随机读,单位毫秒
A | V | S | C | |
---|---|---|---|---|
总时间 | 1965 | 4609 | 9087 | 9000 |
最大时间 | 213 | 518 | 917 | 976 |
最小时间 | 190 | 447 | 903 | 883 |
6,结论
单线程写入性能:A > S > V >>>> C
单线程顺序读取性能:A >> C > S > V
单线程随机读取性能:A > V > S = C
COW单线程顺序读取性能较好,随机读取性能较差,单线程写入性能极差。
Vector单线程随机读取性能较好,顺序读取性能和写入性能较差。
后面再改进一下,测试多线程读/写/混合读写情况下的比较。
标签:同步,min,int,max,sum,List,System,new,比较 来源: https://www.cnblogs.com/CounterX/p/16400925.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。