Java: Synchronizing on primitives?

前端 未结 10 2002
小鲜肉
小鲜肉 2021-02-01 21:13

In our system, we have a method that will do some work when it\'s called with a certain ID:

public void doWork(long id) { /* ... */ }

Now, this

10条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-01 22:00

    Wouldn't it be enough to use a SynchronizedHashMap or Collections.synchronizedMap(Map m) from the java.util.concurrent package instead of a plain HashMap where calls for retrieving and inserting are not synchronized?

    something like:

    Map myMap = new HashMap();
    Map mySyncedMap=Collections.synchronizedMap(myMap);
    

提交回复
热议问题