publicissapient-france / code-elevator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The server crashes because elevatorGames is not thread safe

demarey opened this issue · comments

The code elevator server crashes with following exceptions:

Exception in thread "Thread-10" java.util.ConcurrentModificationException
at java.util.TreeMap.forEach(TreeMap.java:1004)
at elevator.server.ElevatorServer.start(ElevatorServer.java:108)
at java.lang.Thread.run(Thread.java:745)

It happens frequently when many users register at the same time.
A quick fix would be to change the definition of elevatorGames in ElevatorServer class to:

private final Map<Player, ElevatorGame> elevatorGames =  Collections.synchronizedSortedMap(new TreeMap<>());

Nice catch.
Concurrency in this project is badly handled. It could be simpler if using a single thread model with a stack of tasks and a global clock.