class BoundedQueue
private final Object q[]
private int size
synchronized Object get()
while (size == 0) wait()
// take o from queue.
notifyAll();
return o
synchronized void put(Object o)
while (size == q.length) wait()
// put o on queue.
notifyAll();
notifyALL()?
notifyALL() every time?
notifyAll() only when waiting threads are possible.
class BoundedQueue
private BQ bq
synchronized
Object get()
while q.empty()
wait()
if q.get(o)
notifyAll()
return o
synchronized void
put(Object o)
while q.full()
wait()
if q.put(o)
notifyAll()
|
class BQ
private Object q[]
private int size
boolean empty()
return size == 0
boolean full()
return size == q.len
boolean put(o)
// put o in q
return size == 1
boolean get(o)
// take o from q.
return
size == q.len - 1
|
abstract class RWTemplate private int ar, aw, wr, ww protected abstract void doRead() protected abstract void doWrite() | |
public void
read()
beforeRead()
try doRead()
finally afterRead()
public void
write()
beforeWrite()
try doWrite()
finally afterWrite()
|
private
synchronized void
beforeRead()
++wr
while (aw + ww != 0)
wait
--wr
++ar
private
synchronized void
afterRead()
--ar
notifyAll()
|
class Queue
Object get() {
... }
void put(Object o) {
... }
|
class ConcQueue
synchronized Object
get()
return super.get()
synchronized void
put(Object o)
super.put(o)
|
Queue q = new ConcQueue()
synchronized void bearEnter() potFull.wait() assert beesInPot == 0
What's wrong?
void bearEnter()
potFull.wait()
synchronized (this)
assert beesInPot == 0
This page last modified on 17 July 2003.