Last active
January 13, 2016 14:02
-
-
Save emartynov/92cc01f44f710bd80bfe to your computer and use it in GitHub Desktop.
ReadWrite locks
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
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