Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1. In the popBottom() method of Fig. 16.11, the bottom field is volatile to assure that in popBottom() the decrement at Line 15 is immediately
1. In the popBottom() method of Fig. 16.11, the bottom field is volatile to assure that in popBottom() the decrement at Line 15 is immediately visible. Describe a scenario that explains what could go wrong if bottom were not declared as volatile.
2. Why should we attempt to reset the bottom field to zero as early as possible in the popBottom() method? Which line is the earliest in which this reset can be done safely? Can our BoundedDEQueueoverflow anyway? Describe how.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 public Runnable popTop() { int[] stamp = new int [1]; int oldTop int oldStamp if (bottom top.get (stamp), newTop = oldTop + 1; stamp [0], newStamp = oldStamp + 1; oldTop) return null; Runnable r = tasks [oldTop]; if (top.compareAndSet (oldTop, newTop, oldStamp, newStamp)) return r; return null; } public Runnable popBottom() { if (bottom == 0) return null; bottom--; Runnable r = tasks [bottom]; stamp = new int [1]; int int oldTop int oldStamp stamp [0], newStamp = oldStamp + 1; if (bottom > oldTop) return r; top.get (stamp), newTop = 0; if (bottom == oldTop) { bottom= 0; if (top.compareAnd Set (oldTop, newTop, oldStamp, newStamp)) return r; } top.set(new Top,newStamp); return null; 28 29 } Figure 16.11 The BoundedDEQueue class: popTop() and popBottom() methods.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
If the bottom field were not declared as volatile there could be visibility issues in a multithreade...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started