synchronized(queueClientList){
Iterator<Client> iterator = queueClientList.iterator();
while (iterator.hasNext()){
Client client = new Client();
client = iterator.next();
try {
Thread.sleep(client.serviceTime);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println(client.ID+ " it`s out");
queueClientList.remove(client);
}
}
This code should iterate through queueClientList
and sleep the Thread for client.serviceTime
milliseconds. After waking up, it should delete the current client from the queueClientList
.
It gives me the ConcurrentModificationException
at client = iterator.next()
line
The declaration of queueClientList is:
public List<Client> queueClientList = Collections.synchronizedList(new ArrayList<Client>());
Why does it throw that error if this whole method is synchronized?