1
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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
biafas
  • 127
  • 7
  • As an aside, this has nothing to do with threading/thread safety, as the concurrent modification all happens within the same thread. – rmlan Apr 01 '17 at 13:37

1 Answers1

0

Problem solved. The problem was, that I was trying to remove from my list , while iterating thorugh it, which is impossible.

biafas
  • 127
  • 7