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 -> {
intrudersList.forEach(Intruder::action);
towersList.forEach(Tower::action);
otherActivesList.forEach(Active::action);
}));
timeline.setCycleCount(Timeline.INDEFINITE);
timeline.play();
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() {
this.getCell().getContent().remove(this);
TimeLine.getInstance().removeIntruder(this);
System.out.println("death of intruder at: " + cell);
}
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.