0

I want each thread to put some data to the map where the thread-id is a key of the map. (for a multi-threading unit test) Is it safe to put to a Map concurrently in general ?

My code looks like this.

public ThreadData getThreadData() {
    Long threadId = Thread.currentThread().getId();
    if( ! mutableMapRegistry.containsKey(threadId)){
        mutableMapRegistry.put(threadId, new  ThreadData());
    }

    return mutableMapRegistry.get(threadId);
}
sysdoni
  • 51
  • 9

0 Answers0