1 class LockedQueuecT> new ReentrantLock(); 2 final Lock lock 4 final Condition
ID: 3737289 • Letter: 1
Question
1 class LockedQueuecT> new ReentrantLock(); 2 final Lock lock 4 final Condition notEmpty lock.newCondition(): int tail, head, count; 3 final Condition notFull lock.newCondition(): 5 final TO items 6 7 public LockedQueue (int capacity) f 8 items (T)new Object[capacity]: 10 public void enq(T x) lock. lock ); 12 try ( 13 while (count items.length) notFul1.await); items(tail)-x; 15 16 if (++tailitems.1ength) 17 18 19 tail 0; ++count; notEmpty.signal); lock.unlock ); ) finally ( 21 23 24 public T deq) 25 lock.lock): 26 try f while (count 0) notEmpty.await); T x items [head]: 28 if (++headitems.1ength) 30 31 32 head 0; --count; notFull.signal): return x; 35 finally ( 36 lock.unlock); 37 38 39 Queue class: a FIFO queue using locks and conditions. There are w condition fields, one to detect when the queue becomes nonempty,and one to it becomes nonfull. Either of these two practices would fix the bounded bufer eon urd 1 error we just described. Each has a small performance penalty, but negligible the cost of a lost wakeup. lava provides built-in support for monitors in the form of Syotfy blocks and methods, as well as built-in wait0, notity0 methods. (See Appendix A.)
Explanation / Answer
Hi bro...
Explanations:-
a) In line number 13, the code is as below
while(count == items.length)
The pupose of code is to check whether we have traversed through all the items or not. So simply we want boolean value, True/False (1/0). As length property returns integer digit for number of items.
So clearly while should be replaced by if condition.
b) Now the task of two methods
Wakes up one waiting thread.
Wakes up all waiting threads.
LockedQueue class is behaving FIFO strategy for which only one Thread has to perform. This will be very next thread comes under FIFO(first in first out) strategy. So its not necessary to replace it with signalAll() method.
Do not replace the signal() method.
void signal()Wakes up one waiting thread.
void signalAll()Wakes up all waiting threads.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.