Skip to content

Instantly share code, notes, and snippets.

@matteobertozzi
Created July 24, 2014 21:11
Show Gist options
  • Select an option

  • Save matteobertozzi/b9e9797a146d645f2595 to your computer and use it in GitHub Desktop.

Select an option

Save matteobertozzi/b9e9797a146d645f2595 to your computer and use it in GitHub Desktop.
Simple Named Lock
private class NamedLock<T> {
private HashSet<T> locks = new HashSet<T>();
public void lock(final T name) {
synchronized (locks) {
while (locks.contains(name)) {
locks.wait();
}
locks.add(name);
}
}
public void unlock(final T name) {
synchronized (locks) {
locks.remove(name);
locks.notifyAll();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment