2

I have the following methods:

public void create(Deploy deploy)
public void update(Deploy deploy)

These two methods work with same resource type, Deploy. I would like to synchronize these two methods, as creating and updating is non-atomic. However, synchronizing methods is too much - I do not want to block access on two different deployId. Users should be able to create two deploys with different deployId in the same time.

What would be the best way to achieve this in Java8? I was thinking in having (weak?) hashmap of used IDs and have lock on them; something like:

String id = deploy.getId();
lockMap.lockWith(id);  // lookup for Lock and creates if missing
...//work
lockMap.release(id)

Is there anything better then this? I guess there is ;)

igr
  • 10,199
  • 13
  • 65
  • 111
  • 1
    I suppose you need some kind of lock stripping. Look at http://stackoverflow.com/questions/11124539/how-to-acquire-a-lock-by-a-key, http://stackoverflow.com/a/28347825/704335 http://stackoverflow.com/a/28347825/704335 – qwazer Apr 07 '17 at 09:13

0 Answers0