Skip to content

Commit

Permalink
ConcurrentModification error: slightly hacky fix (#12791)
Browse files Browse the repository at this point in the history
* ConcurrentModification error: slightly hacky fix

Should be a fix for #12789
This is not an ideal fix, and we likely have this problem in other areas.
It is possible this is a non-fix too, we should watch out for further error
reports to check carefully if this was a non-fix.

* Formatting
  • Loading branch information
DanVanAtta authored Jul 29, 2024
1 parent 443652c commit 2163a23
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -145,15 +146,23 @@ private void updateStep() {
}
}

SwingUtilities.invokeLater(
() -> {
if (showUnitsToPlace) {
unitsToPlacePanel.setUnits(unitsToPlace);
if (showUnitsToPlace) {
// Small hack: copy the unit list before passing it to a new thread.
// This is to prevent ConcurrentModification. If the 'unitsToPlace' list is modified
// later in this thread, before "SwingUtilities.invokeLater" can execute and complete,
// then we will get a ConcurrentModification exception.
// Ideally we would not modify the 'unitsToPlace' collection again except when
// the swing thread signals that the user has taken action.. Short of that, we create a copy
// here.
final Collection<Unit> unitsToPlaceCopy = new ArrayList<>(unitsToPlace);
SwingUtilities.invokeLater(
() -> {
unitsToPlacePanel.setUnits(unitsToPlaceCopy);
SwingComponents.redraw(unitsToPlacePanel);
} else {
unitsToPlacePanel.removeAll();
}
});
});
} else {
SwingUtilities.invokeLater(unitsToPlacePanel::removeAll);
}
}

@Override
Expand Down

0 comments on commit 2163a23

Please sign in to comment.