Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

182 Chapter8 Monitors and Blocking Synchronization 1 class LockedQueue { 2 final Lock lock new ReentrantLock); final Condition notFull lock.newCondition() final Condition notEmpty - lock.newCondition;

image text in transcribed
182 Chapter8 Monitors and Blocking Synchronization 1 class LockedQueue { 2 final Lock lock new ReentrantLock); final Condition notFull lock.newCondition() final Condition notEmpty - lock.newCondition; 5 final Tl items: 6 int tail, head, count; 7 public LockedQueue(int capacity) f 8 items (T)new object[capacity]: 10 public void enq(T x) { 11 lock.lock): 12 try I 13 while (count items.1ength) notFul1.await); items [tail] x; 14 15 16 if (Htailitems.1ength) tail 0; 18 19 20 finally l ++count; notEmpty.signa1): lock.unlock); 23 24 public T de) I 25 1ock.lockO; 26 try f 27 28 29 30 while (count 0) notEmpty.await) Tx items [head]; if (++head items.length) head 0: .-count; 32 33 notFull.signal): 34 35 finally ( 36 lock.unlock()O: return x; 38 39 Figure 8.5 The LockedQueue class: a FIFO queue using locks and condisions Thet condition fields, one to detect when the queue becomes nonempcyx it becomes nonfull. Either of these two practices would fix the bounded bighe described. Each has a small performance penalty the cost of a lost wakeup. lava provides built-in support for monitors in dre ty blocks and methods, as well as built-in wait(), not methods. (See Appendix A.) 182 Chapter8 Monitors and Blocking Synchronization 1 class LockedQueue { 2 final Lock lock new ReentrantLock); final Condition notFull lock.newCondition() final Condition notEmpty - lock.newCondition; 5 final Tl items: 6 int tail, head, count; 7 public LockedQueue(int capacity) f 8 items (T)new object[capacity]: 10 public void enq(T x) { 11 lock.lock): 12 try I 13 while (count items.1ength) notFul1.await); items [tail] x; 14 15 16 if (Htailitems.1ength) tail 0; 18 19 20 finally l ++count; notEmpty.signa1): lock.unlock); 23 24 public T de) I 25 1ock.lockO; 26 try f 27 28 29 30 while (count 0) notEmpty.await) Tx items [head]; if (++head items.length) head 0: .-count; 32 33 notFull.signal): 34 35 finally ( 36 lock.unlock()O: return x; 38 39 Figure 8.5 The LockedQueue class: a FIFO queue using locks and condisions Thet condition fields, one to detect when the queue becomes nonempcyx it becomes nonfull. Either of these two practices would fix the bounded bighe described. Each has a small performance penalty the cost of a lost wakeup. lava provides built-in support for monitors in dre ty blocks and methods, as well as built-in wait(), not methods. (See Appendix A.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions