Java Reference
In-Depth Information
public void getWriteLock()
{
synchronized(mutex)
{
waitingWriters++;
while(givenLocks != 0)
{
mutex.wait();
}
waitingWriters--;
givenLocks = -1;
}
}
When requesting a write lock, a thread signals that it is waiting by increment-
ing the waitingWriters variable, waits until no more readers are holding the
lock, and then takes a lock by setting givenLocks to -1 and decrements wait-
ingWriters to signal that it is no longer waiting:
public void releaseLock();
{
synchronized(mutex)
{
if(givenLocks == 0)
return;
if(givenLocks == -1)
givenLocks = 0;
else
givenLocks--;
mutex.notifyAll();
}
}
}
To release a lock, the protocol is followed in reverse. To use this lock, an appli-
cation must:
Create a lock for each critical resource to be protected.
Request the lock for read before reading from the resource.
Release the lock for read after reading from the resource.
Request the lock for write before writing to the resource.
Release the lock for write after writing to the resource.
Search WWH ::




Custom Search