I have a multithreading app. Multiple threads are putting things into a Map in which each thing must have a unique ID. Right now I'm using a TreeMap for this purpose like this:
TreeMap<Integer, Thing> things = new TreeMap<>();
things.put(things.isEmpty() ? 0 : things.lastKey() + 1, thing);
But a TreeMap is not thread-safe, so I decided to replace it with a ConcurrentHashMap.
But how could I achieve the same using a HashMap? So how to generate a new unique key for each thing I put into it?