Classe Produtor

/*****************************************************
* 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 produtor, implementada como thread
class P extends Thread {
    private B buffy; // indica a área crítica
    private int psleep;
    private int ID; // Identificador desse objeto

    public P (B b, int psleep, int ID) {
        buffy = b;
        this.psleep = psleep;
        this.ID = ID;
    }

    public void run() {
        while (this.isAlive()) { // semelhante a while (true)
            buffy.add(ID); // consome uma unidade do buffer
            try {
                sleep((int)(Math.random() * psleep)); // dorme por um período randômico
                                                                            // Simula um desescalonamento
            } catch (InterruptedException e) {
                return;
            }
        }
    }
}