3 回答

TA贡献1815条经验 获得超13个赞
提示:
你需要一个互斥锁;例如原始对象锁。
您需要一个当前持有逻辑读锁的读者数量的计数器。
您需要一个标志来说明编写器是否持有逻辑写锁。
当且仅当您正在获取或释放逻辑锁时,您才持有互斥锁。一旦获得它,就释放互斥量。
您将需要使用
wait
和notify
。
实际上,您需要1实施简化版本ReadWriteLock
。
1 - ... 为了你的家庭作业。在真实世界的程序中,您应该简单地使用现有的ReadWriteLock
类。

TA贡献1856条经验 获得超11个赞
在这里回答您更新的代码是您需要完成的一些框架:
public class SharedResource {
private final Object signal = new Object();
private boolean writeLocked;
private int readerCount;
public void write(final Object newState) throws InterruptedException {
this.acquireWriteLock();
try {
// Now we know that no read and no other write is going on.
System.out.println("Write thread has the lock.");
this.doWrite(newState);
} finally {
// make sure we release the lock in any case.
this.realeaseWriteLock();
}
}
private void acquireWriteLock() throws InterruptedException {
synchronized (this.signal) {
// Wait until no more readers *and* no writer holds the lock.
// To do: Insert the condition we need to wait for:
while (/* condition here! */ ) {
// To do: Wait for the lock-holding thread(s) to signal that they released their lock(s).
}
this.writeLocked = true; // Let others know that the write lock has been taken.
}
}
private void realeaseWriteLock() {
synchronized (this.signal) {
this.writeLocked = false;
// To do: Notify any and all other waiting threads that we released the lock!
}
}
public Object read() {
// To be done...
}
private void acquireReadLock() throws InterruptedException {
synchronized (this.signal) {
// Wait until no *writer* holds the lock.
// To do: Insert condition we need to wait for:
while (/* condition here! */ ) {
// To do: Wait for the lock-holding thread(s) to signal that they released their lock(s).
}
// Now we know that no writer holds the lock. Acquire (another) read lock:
this.readerCount++;
}
}
private void releaseReadLock() throws InterruptedException {
synchronized (this.signal) {
this.readerCount--;
// To do: Notify any threads waiting (i.e. writer threads).
// (In fact only *required* if there are *no* more readers now because that's the only condition any thread will wait on.)
}
}
private void doWrite(final Object newState) {
// do stuff
}
private Object doRead() {
return "";
}
}
要理解的要点可能是每次尝试获取锁都可能必须wait,并且每次释放锁都应该有notify任何(潜在的)等待线程。

TA贡献1828条经验 获得超3个赞
此外,我正在考虑使用布尔标志,例如 canReadContinue
你在正确的轨道上。但是请记住,任何数量的线程都可以同时执行它们的读取访问,并且只有在当前没有其他线程正在读取或写入的情况下才能进行写入访问。
所以你需要跟踪当前有多少读者持有锁,并且每个读者都必须确保在完成后释放锁。只有当 0 个读者(和 0 个写者)持有锁时,一个写者才可以继续;并且仅当 & 当 0 个写入者持有锁时,任何读取者都可以继续。
添加回答
举报