IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!
1.传统集合类在并发访问时的问题说明,见例子程序
2.传统方式下用Collections工具类提供的synchronizedCollection方法来获得同步集合,分析该方法的实现源码。
3.传统方式下的Collection在迭代集合时,不允许对集合进行修改。
(1)用空中网面试的同步级线程题进行演示
(2)根据AbstractList的checkForComodification方法的源码,分析产生ConcurrentModificationException异常的原因。
(3)Java5中提供了如下一些同步集合类:
4.通过看java.util.concurrent包下的介绍可以知道有哪些并发集合
(1)ConcurrentHashMap
(2)CopyOnWriteArrayList
(3)CopyOnWriteArraySet
例子程序:
package edu.java5.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.CopyOnWriteArrayList;
public class CollectionModifyException {
public static void main(String[] args) {
//将ArrayList改为CopyOnWriteArrayList,则不会出现以下情况
Collection users = new ArrayList();
//Collection users = new CopyOnWriteArrayList();
users.add(new User("张三",28));
users.add(new User("李四",25));
users.add(new User("王五",31));
Iterator itrUsers = users.iterator();
while(itrUsers.hasNext()){
User user = (User)itrUsers.next();
if("张三".equals(user.getName())){ //代码1
/**
* 注意比较这两种的区别
* users.remove(user)操作可能会引起异常,集合中的数据会被删除
* itrUsers.remove()操作不会引起异常,集合中的数据会被删除
**/
users.remove(user); //代码2
//itrUsers.remove(); //代码3
} else {
System.out.println(user);
}
}
System.out.println(users.size());
}
}
(1)代码1处为 if("张三".equals(user.getName())){ 执行:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at edu.java5.util.CollectionModifyException.main(CollectionModifyException.java:16)
(2)代码1处为 if("李四".equals(user.getName())){ 执行:
{name:'张三',age:28}
2
(3)代码1处为 if("王五".equals(user.getName())){ 执行:
{name:'张三',age:28}
{name:'李四',age:25}
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:372)
at java.util.AbstractList$Itr.next(AbstractList.java:343)
at edu.java5.util.CollectionModifyException.main(CollectionModifyException.java:16)
(4)注释掉代码2,去掉代码3的注释 执行:
{name:'张三',age:28}
{name:'李四',age:25}
2
(1)(2)(3)异常现象原因:
private class Itr implements Iterator<E> {
/**
* Index of element to be returned by subsequent call to next.
*/
int cursor = 0; //代码
/**
* Index of element returned by most recent call to next or
* previous. Reset to -1 if this element is deleted by a call
* to remove.
*/
int lastRet = -1;
/**
* The modCount value that the iterator believes that the backing
* List should have. If this expectation is violated, the iterator
* has detected concurrent modification.
*/
int expectedModCount = modCount; //代码
public boolean hasNext() {
return cursor != size(); //代码
}
public E next() {
checkForComodification();
try {
E next = get(cursor);
lastRet = cursor++; //代码
return next;
} catch (IndexOutOfBoundsException e) {
checkForComodification();
throw new NoSuchElementException();
}
}
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification(); //代码
try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}
final void checkForComodification() {
if (modCount != expectedModCount) //代码
throw new ConcurrentModificationException();
}
}