Classe Buffer - Gerenciador da Memória Crítica

/*****************************************************
* Programa Produtor-Consumidor
* Autor: Luiz Angelo Estefanel
* JDK 1.0.2 - Kawa 2.05a
* Setembro de 1997
* http://www.inf.ufsm.br/~lae/prodcons.html
*****************************************************/

// Classe Buffer...
import java.awt.Scrollbar;
import java.awt.Label;

class B {
    private int Elemento_Critico=0; // área crítica
    private boolean full=false, empty=true; // variáveis de condição
    private Scrollbar barra;
    private Label status;
    private Label acao;
    private int Lenght;
    private int sleeptime;

    // Construtor
    public B (Scrollbar barra, Label status, Label nome1, int N, int sleeptime) {
        this.barra = barra;
        this.status = status;
        this.acao = nome1;
        Lenght = N;
        this.sleeptime = sleeptime;
    }

    // método sincronizado que incrementa o buffer
    public synchronized void sub(int ID) {
        acao.setText("Consumidor " + ID);
        while (empty == true) { // testa condições
            try {
                acao.setText("Consumidor " + ID + ": Dormir");
                wait(); // vai dormir
                acao.setText("Consumidor " + ID);
            } catch (InterruptedException e){
                return;
            }
        }
        --Elemento_Critico;
        if (Elemento_Critico==0) {
            empty = true;
        }
        if (Elemento_Critico==(Lenght-1)) {
            full = false;
            notify();
            acao.setText("Consumidor " + ID + ": Acordar Produtor");
        }
        atualiza();
    }

    public synchronized void add (int ID) {
        acao.setText("Produtor " + ID);
        while (full == true) {
            try {
                acao.setText("Produtor " + ID + ": Dormir");
                wait(); // vai dormir
                acao.setText("Produtor " + ID);
            } catch (InterruptedException e){
                return;
            }
        }
        ++ Elemento_Critico;
        if (Elemento_Critico == Lenght) {
            full = true;
        }
        if (Elemento_Critico==1) {
            empty = false;
            notify();
            acao.setText("Produtor " + ID + ": Acordar Consumidor");
        }
        atualiza ();
    }

    public void atualiza () {
        barra.setValue(Elemento_Critico);
        status.setText(String.valueOf(Elemento_Critico));
        try {
            Thread.sleep(sleeptime);
        } catch (InterruptedException e) {
            return;
        }
    }
}