#1 How to use MultipleReadersWritersMonitor

open
nobody
None
5
2002-08-29
2002-08-29
Aaron Hamid
No

I do not see any example of how to use the
MultipleReadersWritersMonitor implementations. The
question I have is, how is one supposed to handle the
InterruptedException that the startReading and
startWriting methods throw? Since a counter is
incremented (number of readers or number of writers)
immediately before wait()ing, I suspect it would not be
wise to put the whole method in a loop like:

<code><pre>
while (true) {
try {
monitor.startReading();
break;
} catch (InterruptedException ie) {
// startReading has incremented local readers
// variable! if we continue, it will continue to
// increment without corresponding decrement!
}
}</pre></code>

Instead, I believe one would have to stick the loop
<i>within</i> the startReading() method (necessitating a
code change to the JHPC classes). e.g.:

<code><pre>public synchronized void startReading()
/* throws InterruptedException */ {
// we no longer throw InterruptedException
// because we have pushed the loop down into
// this method

nrtotal++;
while (true) {
try {
while (nw!=0) wait();
} catch (InterruptedException ie) {
// safe to continue in loop
// if we want to rethrow the exception
// I would think we need to decrement nrtotal
// to "back out" of the attempted startReading
}
}
nr++;
}</pre></code>

Discussion