@Test
public void testVectorConcurrentReadWrite() {
Vector<Integer> vector = new Vector<>();
vector.add(1);
vector.add(2);
vector.add(3);
vector.add(4);
vector.add(5);
for (Integer item : vector) {
new Thread(vector::clear).start();
System.out.println(item);
}
}
public synchronized Iterator<E> iterator() {
//Itr是AbstractList的私有内部类
return new Itr();
}
synchronized (vector) {
for (int i = 0; i < vector.size(); i ) {
System.out.println(vector.get(i));
}
//或者
synchronized (vector) {
for (Integer item : vector) {
System.out.println(item);
}
public boolean add(E e) {
final ReentrantLock lock = this.lock;
lock.lock();
try {
Object[] elements = getArray();
int len = elements.length;
Object[] newElements = Arrays.copyOf(elements, len 1);
newElements[len] = e;
setArray(newElements);
return true;
} finally {
lock.unlock();
}
}
final void setArray(Object[] a) {
array = a;
}
public E get(int index) {
return get(getArray(), index);
}
final Object[] getArray() {
return array;
}
public Iterator<E> iterator() {
return new COWIterator<E>(getArray(), 0);
}
@Test
public void testThreadSafeListWrite() {
List<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
List<Integer> synchronizedList = Collections.synchronizedList(new ArrayList<>());
StopWatch stopWatch = new StopWatch();
int loopCount = 10000;
stopWatch.start();
/**
* ThreadLocalRandom:是JDK 7之后提供并发产生随机数,能够解决多个线程发生的竞争争夺。
* ThreadLocalRandom不是直接用new实例化,而是第一次使用其静态方法current()。
* 从Math.random()改变到ThreadLocalRandom有如下好处:我们不再有从多个线程访问同一个随机数生成器实例的争夺。
*/
IntStream.rangeClosed(1, loopCount).parallel().forEach(
item -> copyOnWriteArrayList.add(ThreadLocalRandom.current().nextInt(loopCount)));
stopWatch.stop();
System.out.println(
"Write:copyOnWriteList: " stopWatch.getTime() ",copyOnWriteList.size()=" copyOnWriteArrayList
.size());
stopWatch.reset();
stopWatch.start();
/**
* parallelStream特点:基于服务器内核的限制,如果你是八核
* 每次线程只能起八个,不能自定义线程池
*/
IntStream.rangeClosed(1, loopCount).parallel().forEach(
item -> synchronizedList.add(ThreadLocalRandom.current().nextInt(loopCount)));
stopWatch.stop();
System.out.println(
"Write:synchronizedList: " stopWatch.getTime() ",synchronizedList.size()=" synchronizedList
.size());
}
@Test
public void testThreadSafeListRead() {
List<Integer> copyOnWriteArrayList = new CopyOnWriteArrayList<>();
List<Integer> synchronizedList = Collections.synchronizedList(new ArrayList<>());
copyOnWriteArrayList.addAll(IntStream.rangeClosed(1, 1000000).boxed().collect(Collectors.toList()));
synchronizedList.addAll(IntStream.rangeClosed(1, 1000000).boxed().collect(Collectors.toList()));
int copyOnWriteArrayListSize = copyOnWriteArrayList.size();
StopWatch stopWatch = new StopWatch();
int loopCount = 1000000;
stopWatch.start();
/**
* ThreadLocalRandom:是JDK 7之后提供并发产生随机数,能够解决多个线程发生的竞争争夺。
* ThreadLocalRandom不是直接用new实例化,而是第一次使用其静态方法current()。
* 从Math.random()改变到ThreadLocalRandom有如下好处:我们不再有从多个线程访问同一个随机数生成器实例的争夺。
*/
IntStream.rangeClosed(1, loopCount).parallel().forEach(
item -> copyOnWriteArrayList.get(ThreadLocalRandom.current().nextInt(copyOnWriteArrayListSize)));
stopWatch.stop();
System.out.println("Read:copyOnWriteList: " stopWatch.getTime());
stopWatch.reset();
stopWatch.start();
int synchronizedListSize = synchronizedList.size();
/**
* parallelStream特点:基于服务器内核的限制,如果你是八核
* 每次线程只能起八个,不能自定义线程池
*/
IntStream.rangeClosed(1, loopCount).parallel().forEach(
item -> synchronizedList.get(ThreadLocalRandom.current().nextInt(synchronizedListSize)));
stopWatch.stop();
System.out.println("Read:synchronizedList: " stopWatch.getTime());
}
END 十期推荐 【281期】滴滴二面:try-catch-finally 和 return 是什么顺序执行的? 【282期】面试官:你能说说 Nacos 的实现原理吗? 【283期】熊大同学的面试回忆录(2.5年开发经验) 【284期】共享锁、排他锁、互斥锁、悲观锁、乐观锁、行锁、表锁、页面锁、不可重复读、丢失修改、读脏数据 【285期】Spring的@Transactional如何实现的(必考) 【286期】面试时被问到Flutter/Dart的HashMap怎么办? 【287期】ArrayList使用forEach遍历的时候删除元素会报错吗? 【288期】面试官:什么是CAP 定理,为什么CAP不能同时被满足? 【289期】面试官:说一下JVM常用垃圾回收器的特点、优劣势、使用场景和参数设置 【290期】为什么不建议使用Java序列化? ? ~