PDA

View Full Version : [JAVA] ConcurrentModificationException... non capisco come rimediare!


afsdfdlecosdfsfdcco
19-12-2007, 12:42
Allora, ho questo metodo che si occupa di togliere da un ArrayList di Download i download completati o cancellati. Il problema è che mi lancia una ConcurrentModificationException (dove ho messo la freccia) e non capisco perche'! Ogni Download e' un Runnable ed il download avviene quindi in parallelo.


public void removeCompletedOrCancelledDownloads()
{
Iterator<Download> iterator = downloadList.iterator();

while (iterator.hasNext())
{
Download download = iterator.next(); <------- questa riga da problemi

System.out.println("staus: " + download.getStatus()); // per il debug

if (download.getStatus() == Download.COMPLETE || download.getStatus() == Download.CANCELLED)
{
clearDownload(downloadList.indexOf(download));
}
}
}

public void clearDownload(int row)
{
downloadList.remove(row);

// Fire table row deletion notification to table.
fireTableRowsDeleted(row, row);
}


Come posso risolvere? Grazie :)

edit: ah, l'eccezione salta fuori solo se ci sono almeno due download nell'array list (non importa se completati o meno). Con un solo download il metodo termina correttamente :|

edit2: ho provato anche a sincronizzare l'accesso all'arraylist ma niente :(

banryu79
19-12-2007, 12:49
Perchè usando l'Iterator in un ciclo while non dovresti mai modificare la List che viene iterata se non tramite l'iterator stesso...
Al massimo puoi rimuovere l'ultimo elemento restituito da iterator.next() tramite iterator.remove()
(vedi codice qui sotto, però non so cosa faccia il tuo metodo clearDownload(), oltre a rimuovere un elemento dalla lista)


if (download.getStatus() == Download.COMPLETE || download.getStatus() == Download.CANCELLED)
{
//clearDownload(downloadList.indexOf(download));
iterator.remove();
}

Inoltre il metodo remove() di iterator ti ritorna l'elemento rimosso dalla lista.

Quindi credo tu possa fare così:

if (download.getStatus() == Download.COMPLETE || download.getStatus() == Download.CANCELLED)
{
clearDownload( iterator.remove());
}

Modificando opportunamento il metodo clearDownload() in modo che non si occupi più della rimozione dell'elemento Download dalla lista e che accetti in ingresso un oggetto di tipo Download (sempre ammesso che clearDownload() ti serva ancora, altrimenti puoi semplicemente eliminarlo :) )



L'indice dell'elemento in ogni caso lo puoi ottenere, prima di chiamare iterator.next(), con:


int nextIndex()
-> Returns the index of the element that would be returned by a subsequent call to next.



Il problema dell'eccezione lanciata è spiegato qui:


The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.



Ciao :)

afsdfdlecosdfsfdcco
19-12-2007, 13:37
...


Grazie mille, adesso ho capito!!

clearDownload mi serve perche' e' chiamato singolarmente quando voglio rimuovere un singolo download (e si occupa anche di aggiornare la table).

Grazie ancora, adesso sistemo!

banryu79
19-12-2007, 14:54
Figurati, è stato un piacere.

Caso vuole che proprio oggi mi son trovato a lavorare con gli Iterator su diversi tipi di Collection e giocoforza ho imparato un po' di cosette su questi simpaticoni.

Ciao :)

cionci
19-12-2007, 16:58
Inoltre ricordati che ArrayList non è thread safe, l'accesso concorrente va sincronizzato ;)