Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the code below, is the waitWhileFull() method (lines 45-51) multithread-safe as written? 1: //... 2: private final int[] slots; 3: private int head; 4:

In the code below, is the waitWhileFull() method (lines 45-51) multithread-safe as written?

 1: //... 2: private final int[] slots; 3: private int head; 4: private int tail; 5: private int count; 6: private final Object lockObject; 7: //... 8: 9: /** 10: * Returns true if added, false for timeout. 11: */ 12: public boolean add(int item, long msTimeout) throws InterruptedException { 13: synchronized ( lockObject ) { 14: if (waitWhileFull(msTimeout)) { 15: slots[tail] = item; 16: tail = (tail + 1) % slots.length; 17: count++; 18: lockObject.notifyAll(); 19: return true; 20: } else { 21: return false; 22: } 23: } 24: } 25: 26: public void add(int item) throws InterruptedException { 27: waitWhileFull(); 28: synchronized ( lockObject ) { 29: slots[tail] = item; 30: tail = (tail + 1) % slots.length; 31: count++; 32: lockObject.notifyAll(); 33: } 34: } 35: 36: /** 37: * Returns true if no longer full, false for a timeout. 38: */ 39: public boolean waitWhileFull(long msTimeout) throws InterruptedException { 40: // In here is code that works correctly 41: // and synchronizes on lockObject 42: // ... 43: } 44: 45: public void waitWhileFull() throws InterruptedException { 46: synchronized ( lockObject ) { 47: while (isFull()) { 48: lockObject.wait(); 49: } 50: } 51: }

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

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions

Question

internationalization of business?

Answered: 1 week ago