Introducing Extensions (Chapter 26)

SOLUTION 26.1The complete code should look something like:
package com.oozinoz.machine;
import java.util.*;
public class BinStack
{
    public static final int STACK_LIMIT = 3;
    private Stack stack = new Stack();
    
    synchronized public Bin pop()
    {
        while (stack.size() == 0)
        {
            try
            {
                wait();
                Thread.sleep(500);
            }
            catch (InterruptedException ignore)
            {
            }
        }
        if (stack.size() == STACK_LIMIT)
        {
            notify();
        }
        return (Bin) stack.pop();
    }
    
    synchronized public void push(Bin b)
    {
        while (stack.size() == STACK_LIMIT)
        {
            try
            {
                wait();
                Thread.sleep(500);
            }
            catch (InterruptedException ignore)
            {
            }
        }
        if (stack.size() == 0)
        {
            notify();
        }
        stack.push(b);
    }
    
    public int size()
    {
        return stack.size();
    }
}
SOLUTION 26.2The call to notify() in the ...

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.