有时候你的应用程序可能会变得过于复杂,某些功能可能具有太多的副作用。此外,也许另一个线程正在与该列表做一些错误的事情,而你无法轻易地找到它所在的位置。
针对我自己的问题,我编写了自己的列表系统,委托另一个列表,并且一旦锁定,所有其他修改都会抛出ConcurrentModificationException异常,因此坏的修改指令将在输出中得到异常。它还可以检测上述错误。
import java.util.*;
/**
* 该类是一个可锁定的列表,用于调试列表上的ConcurrentModificationException。
* 可以使用setLocked(boolean)方法来打开/关闭锁定。当锁定时,所有对列表或迭代器的写访问都会引发ConcurrentModificationException异常。
* 简单的使用案例:
*
* list.setLocked(true);
*
* for (Object o : list.iterator()) // 现在不会引发ConcurrentModificationException异常,而会抛出其他引起此异常的指令
* { ... }
*
* list.setLocked(false);
*/
public class LockableList<E> implements List<E> {
protected class LockableListIterator implements Iterator<E> {
protected Iterator<E> iterator;
public LockableListIterator(Iterator<E> iterator) {
this.iterator = iterator;
}
public boolean hasNext() {
return iterator.hasNext();
}
public E next() {
return iterator.next();
}
public void remove() {
checkLock();
iterator.remove();
}
}
protected class LockableListListIterator implements ListIterator<E> {
protected ListIterator<E> listIterator;
public LockableListListIterator(ListIterator<E> listIterator) {
this.listIterator = listIterator;
}
public boolean hasNext() {
return listIterator.hasNext();
}
public E next() {
return listIterator.next();
}
public boolean hasPrevious() {
return listIterator.hasPrevious();
}
public E previous() {
return listIterator.previous();
}
public int nextIndex() {
return listIterator.nextIndex();
}
public int previousIndex() {
return listIterator.previousIndex();
}
public void remove() {
checkLock();
listIterator.remove();
}
public void set(E e) {
checkLock();
listIterator.set(e);
}
public void add(E e) {
checkLock();
listIterator.add(e);
}
}
protected class LockableListSubList implements List<E>
{
protected List<E> list;
public LockableListSubList(List<E> list) {
this.list = list;
}
public int size() {
return list.size();
}
public boolean isEmpty() {
return list.isEmpty();
}
public boolean contains(Object o) {
return list.contains(o);
}
public Iterator<E> iterator() {
return new LockableListIterator(list.iterator());
}
public Object[] toArray() {
return list.toArray();
}
public <T> T[] toArray(T[] a) {
return list.toArray(a);
}
public boolean add(E e) {
checkLock();
return list.add(e);
}
public boolean remove(Object o) {
checkLock();
return list.remove(o);
}
public boolean containsAll(Collection<?> c) {
return list.containsAll(c);
}
public boolean addAll(Collection<? extends E> c) {
checkLock();
return list.addAll(c);
}
public boolean addAll(int index, Collection<? extends E> c) {
checkLock();
return list.addAll(index, c);
}
public boolean removeAll(Collection<?> c) {
checkLock();
return list.removeAll(c);
}
public boolean retainAll(Collection<?> c) {
checkLock();
return list.retainAll(c);
}
public void clear() {
checkLock();
list.clear();
}
@Override
public boolean equals(Object o) {
return list.equals(o);
}
@Override
public int hashCode() {
return list.hashCode();
}
public E get(int index) {
return list.get(index);
}
public E set(int index, E element) {
checkLock();
return list.set(index, element);
}
public void add(int index, E element) {
checkLock();
list.add(index, element);
}
public E remove(int index) {
checkLock();
return list.remove(index);
}
public int indexOf(Object o) {
return list.indexOf(o);
}
public int lastIndexOf(Object o) {
return list.lastIndexOf(o);
}
public ListIterator<E> listIterator() {
return new LockableListListIterator(list.listIterator());
}
public ListIterator
使用方法如下:
List list = new LockableList(new ArrayList(...));
list.setLocked(true);
for (E e : list.iterator())
{ ... }
list.setLocked(false);
希望对他人有所帮助。