Skip to content

Instantly share code, notes, and snippets.

@emartynov
Last active January 13, 2016 14:02
Show Gist options
  • Save emartynov/92cc01f44f710bd80bfe to your computer and use it in GitHub Desktop.
Save emartynov/92cc01f44f710bd80bfe to your computer and use it in GitHub Desktop.
ReadWrite locks
public Object read() {
synchronized(writeLock) {
synchronized(readLock) {
readersCount += 1;
}
}
final Object result = value;
synchronized(readLock) {
readersCount -= 1;
readLock.notify();
}
return result;
}
public void write(Object newValue) {
synchronized(writeLock) {
synchronised(readLock) {
if (readersCount > 0) {
readersCount.wait();
}
}
value = newValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment