Skip to content

Instantly share code, notes, and snippets.

@jklingsporn
Created May 9, 2017 07:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jklingsporn/f325c909a64b4e73bfae8bee10df5f0a to your computer and use it in GitHub Desktop.
Save jklingsporn/f325c909a64b4e73bfae8bee10df5f0a to your computer and use it in GitHub Desktop.
private static Map<Integer,Object> LOCKS = new ConcurrentHashMap<>();
private static void mayLock(Integer arg) {
logger.info("entering method {} ", arg);
synchronized (LOCKS.computeIfAbsent(arg,f->new Object())){
logger.info("entering lock "+Thread.currentThread().getName());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment