同步容器是通过synchronized来实现同步的,所以性能较差。而且同步容器也并不是绝对线程安全的,在一些特殊情况下也会出现线程不安全的行为。那么有没有更好的方式代替同步容器呢?----> 那就是并发容器,有了并发容器后同步容器的使用也越来越少的,大部分都会优先使用并发容器(J.U.C)。
文章目录
并发容器
传统容器与并发容器之间的对应关系
并发容器特性
- CopyOnWriteArrayList、CopyOnWriteArraySet因为需要copy数组,需要消耗内存,可能引发yonggc或者 fullgc,并且不能做到实时性,适合读多写少的情景
- ConcurrentSkipListSet 支持自然排序,并且可以在构造的时候自己定义比较器,可以保证每一次的操作是原子性的,比如add、remove等,但是对于批量操作,如addAll()等并不能保证原子性(需要自己手动做同步操作,如加锁等)
- ConcurrentHashMap针对读操作做了大量的优化,这个类具有特别高的并发性,高并发场景下有特别好的表现
- ConcurrentSkipListMap与ConcurrentHashMap相比的key是有序的,它支持更高的并发,它的存取时间和线程数是没有关系的,在一定的数据量下,并发的线程越多ConcurrentSkipListMap越能体现出它的优势来
CopyOnWriteArrayList实例
package cn.wideth.util;
import java.util.List;
import java.util.concurrent.*;
public class MyCopyOnWriteArrayList {
//请求总数
private static int acquireTotal = 10000;
//同时并发执行的线程数目
private static int threadTotal = 200;
private static List<Integer> list = new CopyOnWriteArrayList<>();
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(acquireTotal);
for(int i = 0; i < acquireTotal; i++){
final int count = i;
es.execute(() -> {
try {
semaphore.acquire();
updateAcquireNumber(count);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
es.shutdown();
System.out.println("请求总数" + "--> " + list.size());
}
private static void updateAcquireNumber(int count) {
list.add(count);
}
}
运行结果
CopyOnWriteArraySet实例
package cn.wideth.util;
import java.util.Set;
import java.util.concurrent.*;
public class MyCopyOnWriteArraySet {
//请求总数
private static int acquireTotal = 10000;
//同时并发执行的线程数目
private static int threadTotal = 200;
private static Set<Integer> set = new CopyOnWriteArraySet<>();
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(acquireTotal);
for(int i = 0; i < acquireTotal; i++){
final int count = i;
es.execute(() -> {
try {
semaphore.acquire();
updateAcquireNumber(count);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
es.shutdown();
System.out.println("请求总数" + "--> " + set.size());
}
private static void updateAcquireNumber(int count) {
set.add(count);
}
}
运行结果
ConcurrentSkipListSet实例
package cn.wideth.util;
import java.util.Set;
import java.util.concurrent.*;
public class MyConcurrentSkipListSet {
//请求总数
private static int acquireTotal = 10000;
//同时并发执行的线程数目
private static int threadTotal = 200;
private static Set<Integer> set = new ConcurrentSkipListSet<>();
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(acquireTotal);
for(int i = 0; i < acquireTotal; i++){
final int count = i;
es.execute(() -> {
try {
semaphore.acquire();
updateAcquireNumber(count);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
es.shutdown();
System.out.println("请求总数" + "--> " + set.size());
}
private static void updateAcquireNumber(int count) {
set.add(count);
}
}
运行结果
ConcurrentHashMap实例
package cn.wideth.util;
import java.util.Map;
import java.util.concurrent.*;
public class MyConcurrentHashMap {
//请求总数
private static int acquireTotal = 10000;
//同时并发执行的线程数目
private static int threadTotal = 200;
private static Map<Integer,Integer> map = new ConcurrentHashMap<>();
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(acquireTotal);
for(int i = 0; i < acquireTotal; i++){
final int count = i;
es.execute(() -> {
try {
semaphore.acquire();
updateAcquireNumber(count);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
es.shutdown();
System.out.println("请求总数" + "--> " + map.size());
}
private static void updateAcquireNumber(int count) {
map.put(count,count);
}
}
运行结果
ConcurrentSkipListMap实例
package cn.wideth.util;
import java.util.Map;
import java.util.concurrent.*;
public class MyConcurrentSkipListMap {
//请求总数
private static int acquireTotal = 10000;
//同时并发执行的线程数目
private static int threadTotal = 200;
private static Map<Integer,Integer> map = new ConcurrentSkipListMap<>();
public static void main(String[] args) {
ExecutorService es = Executors.newCachedThreadPool();
final Semaphore semaphore = new Semaphore(threadTotal);
final CountDownLatch countDownLatch = new CountDownLatch(acquireTotal);
for(int i = 0; i < acquireTotal; i++){
final int count = i;
es.execute(() -> {
try {
semaphore.acquire();
updateAcquireNumber(count);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
es.shutdown();
System.out.println("请求总数" + "--> " + map.size());
}
private static void updateAcquireNumber(int count) {
map.put(count,count);
}
}
运行结果
本文小结
本文给出了juc中几种常见的集合类的代码实现,具体的API请看对应的文档。