class Queue { private class Element { int info; Element next; Element(int n) { info = n; next = null; } } private Element front; private Element rear; Queue() { front = rear = null; } public synchronized void insert(int n){ if (front == null) { front = rear = new Element(n); notifyAll(); } else { rear = rear.next = new Element(n); } } public synchronized int pop(){ try { while (front == null) wait(); } catch (InterruptedException e) { return 0; // We could do a better treatment of the case } // of interruption, but it's not the object of the exercise int n = front.info; front = front.next; if (front == null) { rear = null; } //this line is not necessary return n; } }
class Boat{ private int remainingCapacity; private int currentWeight; public Boat(int c) { remainingCapacity = c; } public synchronized void getIn(int k) throws InterruptedException { while (remainingCapacity < k) wait(); remainingCapacity = remainingCapacity - k; } public synchronized void getOut(int k) { remainingCapacity = remainingCapacity + k; notifyAll(); //Note that notify() would not work here }
fun aux p [] n = [] | aux p (x::L) n = (if p x then [n] else []) @ (aux p L (n+1)); fun positions p L = aux p L 1;
fun apply_repeatedly 0 f x = x | apply_repeatedly n f x = f(apply_repeatedly (n-1) f x);