aran aran - 1 year ago 203
Java Question

java.util.ConcurrentModificationException in javafx timeline

I haven't made Thread myself.
I have one timeline that runs from the beginning to the end of the program which is as follows:

Timeline timeline = new Timeline(
new KeyFrame(Duration.millis(TIMELINE_DELAY), event -> {

And when the die method of the Intruder class is called, i get this Concurrent Modification Exception.

First, I don't actually understand how timeline works! Does it create new Threads itself or what? and what will happen if for example we have a timeline that does a task every 10 seconds and that task takes 15 seconds to be done!
And second indeed: How can i get this fixed!?

public void die() {
System.out.println("death of intruder at: " + cell);

Answer Source

Many thanks to @DVarga, This is what the problem was and how i fixed it: the problem was that I was modifying the intrudersList in removeIntruder , while iterating on it in KeyFrame. And I solved it by wrapping the body of the removeIntruder method in a Platform.runLater(() -> { ... }) block. I Guess what Platform.runLater does is that it waits until the list is modifyable and then modifies it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download