I'm trying to implement a Lamport distributed clock system and I have problems when it comes to the list where I store the petitions received and sent. Of course this list will have concurrency problems as both the threads in charge of outwards and incoming communication make use of it, but I'm unable to avoid the exception.
The exception occurs in various places, but I will only post one function as a reference. I tried making both the function and the list syncronized, but I still get the exception and I have no idea why:
private synchronized int getMyRequestedClock(String process) {
LinkedList<LamportRequest> lamportRequest = parent.getLamportQueue();
synchronized (lamportRequest){
for (LamportRequest l : lamportRequest){ //here is the exception caused
if (l.getProcess().equals(process)){
return l.getClock();
}
}
}
return 0;
}
I've seen many posts stating that usually this is caused when removing an item from a list whilst you are iterating it: Iterating through a Collection, avoiding ConcurrentModificationException when removing objects in a loop
I modified my remove code to use the Iterator, but I still get the exception. What follows is my current remove function without the Iterator, as it didn't solve the problem:
public synchronized void releaseRequest(String releaseProcess) {
synchronized (lamportQueue){
lamportQueue.removeIf(lr -> lr.getProcess().equals(releaseProcess));
}
//debug prints for myself in order to check that the queue values are correct
System.out.println();
for (LamportRequest lr : lamportQueue){
System.out.println("[LAMPORT (remove)]" + lr.toString());
}
removed = true;
}
How can this problem be fixed?
Thanks in advance