これが私の問題です:
このデータ構造の が存在する間に が変更さjava.util.ConcurrentModificationException
れるため、このコードは をスローします。java-doc によると、このコンテナーはフェイルファスト イテレーターのみを提供します。Vector
listeners
Iterator
その「ライブ」中に要素が削除された場合、無効にならない(フェイルファストではない)を提供する、またはJavaIterator
のような標準コンテナを乗り越える可能性はありますか?Vector
List
Iterator
Iterator
std::list
C++の場合と同じように動作する必要があります。そこでは、現在のイテレータが削除されていても、イテレータは常に有効です。イテレータがリスト内の次の要素に設定されるよりも。
public class ClientHandle {
private final Vector<ClientHandleListener> listeners = new Vector<ClientHandleListener>();
public synchronized void addListener(ClientHandleListener chl) {
listeners.add(chl);
}
public synchronized void removeListener(ClientHandleListener chl) {
listeners.remove(chl);
}
private void fireConnectionClosed() {
final ClientHandle c = this;
final Iterator<ClientHandleListener> it = listeners.iterator();
new Thread(){
@Override
public void run() {
while (it.hasNext()) {
it.next().connectionClosed(c); //FIXME the iterator gets modified
}
};
}.start();
}}
public class ClientHandlePool implements ClientHandleListener, TaskManagerListener {
/*...*/
public synchronized void removeClientHandle(ClientHandle ch) {
//here the listeners Vector from the ClientHandle gets modified
ch.removeListener(this);
ch.removeListener(currentListener);
clientHandles.remove(ch);
}
@Override
public void connectionClosed(ClientHandle ch) {
removeClientHandle(ch);
}
}