O'Reilly logo

Design Patterns Java™ Workbook by Steven John Metsker

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required