Is it also possible to get in an infinite loop when concurrently iterating and writing with at most one concurrent writer?
I would say a cautious no: these infinite loops occur because multiple threads are re-wiring the relationships between the nodes, and so may make conflicting updates. A single thread won't conflict with itself, so such a re-wiring mixup would not occur.
However, I am not confident in this - but I don't need to be: such a usage of a TreeMap
is in violation of the documentation:
If multiple threads access a map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally.
If you don't externally synchronize, the behavior is undefined. Implementors of the class are free to implement the class in any way that meets the specification; and it is possible that one way might result in an infinite loop.
If you are encountering an infinite loop in a TreeMap
, that's a symptom, not the root cause - namely, unsynchronized access to mutable data. This means that there is no guarantee that the values being read by the only-reading threads are correct either.