1.古老的同步类容器像:Vector(List),HashTable(Map),都是对整个数据结构加了锁,性能最低
2.通过封装,将非锁的容器定义为加锁的容器
对于非线程安全的list,对同一个位置处并发写入,则可能会覆盖,所以次数是小于1万
package com.caolh.middle;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class DemoThread26 {
public static void main(String[] args) throws Exception {
/*
* 使用以下方法被包裹的类将支持多线程
* Collections.synchronizedCollection(c);
* Collections.synchronizedList(list);
* Collections.synchronizedMap(m);
* Collections.synchronizedSet(s);
* Collections.synchronizedSortedMap(m);
* Collections.synchronizedSortedSet(s);
*/
// 非线程安全的List
final List<String> list = new ArrayList<String>(1);
//线程安全的List
//final List<String> list = Collections.synchronizedList(new ArrayList<String>());
ExecutorService es = Executors.newFixedThreadPool(100);
//向list中并发加入1万个元素,如果是线程安全的那么list.size=1万,否则!=1万
for (int i = 0; i < 10000; i++) {
es.execute(new Runnable() {
@Override
public void run() {
list.add("5");
}
});
}
es.shutdown();
while (true) {
if (es.isTerminated()) {
System.out.println("所有的子线程都结束了!");
System.out.println(list.size());
if(list.size()!=10000){
System.out.println("线程不安全!");
}else{
System.out.println("线程安全!");
}
break;
}
}
}
}
3.ConcurrentHashMap
将锁的粒度降低了,分了16个segment区段 ,单个的segment 会加锁。假设t1线程访问s1区段,t2 访问s2区段,那么t1和t2 是不存在锁竞争的,如果此时t3 也访问s1区段,那么t3则会等待t1的锁。
package com.caolh.middle;
import java.util.Collections;
import java.util.Hashtable;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CountDownLatch;
class DemoDemo{
public DemoDemo() {
}
}
public class DemoThread27 {
//通过并发下的运行时间对比ConcurrentHashMap与Hashtable的性能
// currenthashmap 单个线程执行速度快
public static void testMap1(){
final ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String,Integer>();
//final Hashtable<String, Integer> map = new Hashtable<String,Integer>();
for(int i=0;i<10;i++){
new Thread(new Runnable() {
@Override
public void run() {
Long start = System.currentTimeMillis();
for(int i=0;i<1000000;i++){
map.put("a"+i, i);
}
System.out.println(System.currentTimeMillis()-start);
}
}).start();
}
}
//对比ConcurrentSkipListMap与SortedMap的性能
public static void testSkipListMap1() throws InterruptedException{
//高性能线程安全
//final ConcurrentSkipListMap<String, DemoDemo> skipMap = new ConcurrentSkipListMap<String,DemoDemo>();
//线程不安全、性能高
//final SortedMap<String, DemoDemo> skipMap = new TreeMap<String,DemoDemo>();
//低性能线程安全
final SortedMap<String, DemoDemo> skipMap = Collections.synchronizedSortedMap(new TreeMap<String,DemoDemo>());
final CountDownLatch countDownLatch = new CountDownLatch(10);
for(int i=0;i<10;i++){
new Thread(new Runnable() {
@Override
public void run() {
Long start = System.currentTimeMillis();
Random rn = new Random();
for(int i=0;i<1000;i++){
try {
skipMap.put("k"+i%10, new DemoDemo());
Thread.sleep(0);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("添加100个元素耗时:"+(System.currentTimeMillis()-start)+"毫秒");
countDownLatch.countDown();
}
}).start();
}
countDownLatch.await();
//System.out.println(skipMap);
System.out.println(skipMap.size());
}
public static void testMap2(){
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<String,Integer>();
map.put("a", 1);
map.put("b", 1);
map.put("c", 1);
map.put("d", 1);
//如果key已经存在则更新
map.put("a", 2);
System.out.println(map);
//如果key存在则不更新,不存在则添加
map.putIfAbsent("b", 2);
map.putIfAbsent("e", 3);
System.out.println(map);
}
public static void testSkipListMap2(){
ConcurrentSkipListMap<String, Integer> map = new ConcurrentSkipListMap<String, Integer>();
map.put("a", 1);
map.put("b1", 1);
map.put("c", 1);
map.put("d", 1);
//如果key已经存在则更新
map.put("a", 2);
System.out.println(map);
//如果key存在则不更新
map.putIfAbsent("b", 2);
System.out.println(map);
}
public static void main(String[] args) throws Exception {
testMap1();
// testMap2();
//testSkipListMap1();
// testSkipListMap2();
}
}