The superscript "(d)" stands for "difficult". Exercises similar to those marked with "(d)" might appear in candidacy exams, but not in the standard exams of CSE 428.
interface Stack_interface(){ void push(int); int pop(); boolean is_empty(); }Hint: you can follow the scheme of the definition of a Queue (FIFO buffer) given in the program Eratosthenes of Lecture 17. Note that there was no method is_empty because, for that particular application, it was not required.
class Stack implements Stack_interface { private class Element { int info; Element next; Element(int n, Element e) { info = n; next = e; } } private Element first; public Stack() { first = null; } public synchronized void push(int n){ first = new Element(n,first); if (first.next == null) then notifyAll(); //if the stack was empty then notify all waiting consumers } public synchronized int pop(){ try { while (first == null) wait(); } catch (InterruptedException e) { return 1; } int n = first.info; first = first.next; return n; } public synchronized boolean is_empty(){ return first == null; } }
if (left_chopstick.is_free() && right_chopstick.is_free()) { left_chopstick.take(); right_chopstick.take(); ... }in Philospopher.try_to_eat(). Suppose that two neighbour philosophers are executing the test at the same time (this is possible even if the method try_to_eat() is synchronized, because the lock is per object, not per method (since it is not static)). Suppose that both the philosophers pass the test: if Chopstick.take() were not synchronized, they could end up in taking both the same chopstick.
synchronized (from) { int k = from.get(); to.put(k); }in Demon.run() with the statement
{ int k = from.get(); to.put(k); }(i.e. we eliminate the synchronization on from). Does Buffer2 still represent a buffer with two positions?
synchronized (from) { int k = from.get(); to.put(k); }in Demon.run() with the statement
if (!to.is_full()) synchronized (from) { int k = from.get(); to.put(k); }What are the consequences?