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 ;)