Created
July 24, 2014 21:11
-
-
Save matteobertozzi/b9e9797a146d645f2595 to your computer and use it in GitHub Desktop.
Simple Named Lock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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