1 Star 0 Fork 0

张念磊/java-base

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ArrayListSecturyTest.java 6.38 KB
一键复制 编辑 原始数据 按行查看 历史
package mrzhang.juc.nien.chapter5;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author zhangnianlei
* @description
* @date 2021/11/25
*/
public class ArrayListSecturyTest {
// 线程池核心线程数
private final static int THREAD_POOL_CORE_SIZE = 20;
// 向数组中添加多少次
private final static int COUNT = 10000000;
public static void main(String[] args) throws InterruptedException {
UnSafeArrayList();
safeArrayList1();
safeArrayList2();
safeArrayList3();
}
private static void UnSafeArrayList() throws InterruptedException {
long start = System.currentTimeMillis();
List<Integer> list = new ArrayList(COUNT);
// 创建一个线程池
ExecutorService e = new ThreadPoolExecutor(THREAD_POOL_CORE_SIZE, THREAD_POOL_CORE_SIZE, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(COUNT));
final CountDownLatch countSign = new CountDownLatch(COUNT);
for (int i = 0; i < COUNT; i++) {
final int finalI = i;
e.execute(new Runnable() {
public void run() {
try {
list.add(finalI);
countSign.countDown();
} catch (Exception ex) {
ex.printStackTrace();
}
}
});
}
countSign.await();
e.shutdown();
long end = System.currentTimeMillis();
System.out.println("不安全的版本");
System.out.println(String.format("用时:%d ms", end - start));
System.out.println(String.format("result: %d", list.size()));
System.out.println();
}
private static void safeArrayList1() throws InterruptedException {
long start = System.currentTimeMillis();
List<Integer> list = new ArrayList(COUNT);
// 创建一个线程池
ExecutorService e = new ThreadPoolExecutor(THREAD_POOL_CORE_SIZE, THREAD_POOL_CORE_SIZE, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(COUNT));
final CountDownLatch countSign = new CountDownLatch(COUNT);
for (int i = 0; i < COUNT; i++) {
final int finalI = i;
e.execute(new Runnable() {
public void run() {
synchronized (list) {
list.add(finalI);
countSign.countDown();
}
}
});
}
countSign.await();
e.shutdown();
long end = System.currentTimeMillis();
System.out.println("安全的版本 - 使用synchronize");
System.out.println(String.format("用时:%d ms", end - start));
System.out.println(String.format("result: %d", list.size()));
System.out.println();
}
private static void safeArrayList2() throws InterruptedException {
long start = System.currentTimeMillis();
List<Integer> list = new ArrayList(COUNT);
// 创建一个线程池
ExecutorService e = new ThreadPoolExecutor(THREAD_POOL_CORE_SIZE, THREAD_POOL_CORE_SIZE, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(COUNT));
final CountDownLatch countSign = new CountDownLatch(COUNT);
ReentrantLock lock = new ReentrantLock();
for (int i = 0; i < COUNT; i++) {
final int finalI = i;
e.execute(new Runnable() {
public void run() {
lock.lock();
try {
list.add(finalI);
countSign.countDown();
} finally {
lock.unlock();
}
}
});
}
countSign.await();
e.shutdown();
long end = System.currentTimeMillis();
System.out.println("安全的版本 - 使用ReentrantLock");
System.out.println(String.format("用时:%d ms", end - start));
System.out.println(String.format("result: %d", list.size()));
System.out.println();
}
private static void safeArrayList3() throws InterruptedException {
long start = System.currentTimeMillis();
List<Integer> list = Collections.synchronizedList(new ArrayList<>(COUNT));
// 创建一个线程池
ExecutorService e = new ThreadPoolExecutor(THREAD_POOL_CORE_SIZE, THREAD_POOL_CORE_SIZE, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(COUNT));
final CountDownLatch countSign = new CountDownLatch(COUNT);
ReentrantLock lock = new ReentrantLock();
for (int i = 0; i < COUNT; i++) {
final int finalI = i;
e.execute(new Runnable() {
public void run() {
list.add(finalI);
countSign.countDown();
}
});
}
countSign.await();
e.shutdown();
long end = System.currentTimeMillis();
System.out.println("安全的版本 - 使用Collections.synchronizedList(new ArrayList<>())");
System.out.println(String.format("用时:%d ms", end - start));
System.out.println(String.format("result: %d", list.size()));
System.out.println();
}
private static void safeArrayList5() throws InterruptedException {
long start = System.currentTimeMillis();
List<Integer> list = new CopyOnWriteArrayList<>();
// 创建一个线程池
ExecutorService e = new ThreadPoolExecutor(THREAD_POOL_CORE_SIZE, THREAD_POOL_CORE_SIZE, 0L, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(COUNT));
final CountDownLatch countSign = new CountDownLatch(COUNT);
ReentrantLock lock = new ReentrantLock();
for (int i = 0; i < COUNT; i++) {
final int finalI = i;
e.execute(new Runnable() {
public void run() {
list.add(finalI);
countSign.countDown();
}
});
}
countSign.await();
e.shutdown();
long end = System.currentTimeMillis();
System.out.println("安全的版本 - 使用CopyOnWriteArrayList");
System.out.println(String.format("用时:%d ms", end - start));
System.out.println(String.format("result: %d", list.size()));
System.out.println();
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/StephenRead/java-base.git
git@gitee.com:StephenRead/java-base.git
StephenRead
java-base
java-base
master

搜索帮助