Make your own free website on Tripod.com

cccccc

Home
Page Title

package lab8;
import java.awt.*;
import java.awt.event.*;

public class P1 extends java.applet.Applet { 
    private int contador;
    private TextField t;
    private boolean puedeConsumir;
    private Productor prod; 
    private Consumidor cons;
   
    public void init()
   
    {  
        add(t = new TextField(10));
        t.setEditable(false);      
    }

    public void start()
    {
        prod = new Productor(this);
        cons = new Consumidor(this);
        prod.start();
        cons.start();
    }
   
    public void stop()
   
    {
        prod.detener();
        cons.detener();
    }
   
    public synchronized void produce()
    {
        t.setText(String.valueOf(++contador));       
        notify();
    }
   
    public synchronized void consume()
   
    {
        if(contador <= 0) {
            try {
                wait();
            } catch(InterruptedException e) {}
        }
        t.setText(String.valueOf(--contador));          
    }
}

 


class Productor 
    extends ProductorConsumidor
{
    public Productor(P1 p1) {
        super(p1);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p1.produce();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

class Consumidor
    extends ProductorConsumidor
   
{
    public Consumidor(P1 p1) {
        super(p1);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p1.consume();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

class ProductorConsumidor
    extends Thread
{
    protected boolean corriendo;                 
    protected P1 p1;
   
    public ProductorConsumidor(P1 p1) {
        this.p1 = p1;       
    }

    public void detener() {
        corriendo = false;
    }
}

/////////////////////////////////
package lab8;

import consola.*;

import consola.*;

public class P2{
    private int cont; // contador
    private int t; // el texto
   
    private Productor productor;  // productor
    private Consumidor consumidor; // consumidor
   
    public static void main(String[] args)
    {
        new P2();
    }
    public P2()
    {  
         productor = new Productor(this);
         consumidor = new Consumidor(this);
         productor.start();
         consumidor.start();
    }
  
    public synchronized void produce() {
        cont++;
        ES.escribe(cont+"\n");
        notify();
    }
   
    public synchronized void consume() {
        cont--;
        if(cont == 0) {
            try {
                wait();
            } catch(InterruptedException e) {}
        }
        ES.escribe(cont+"\n");
    }
}

class ProductorConsumidor
    extends Thread
{
    protected boolean corriendo;                 
    protected P2 p2;
   
    public ProductorConsumidor(P2 p2) {
        this.p2 = p2;       
    }

    public void detener() {
        corriendo = false;
    }
}

class Productor  extends ProductorConsumidor {
    public Productor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.produce();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

class Consumidor extends ProductorConsumidor {
    public Consumidor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.consume();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

 

 


/////////////////////////////////
package Hilo2;
import java.awt.*;
import java.awt.event.*;

public class P1 extends java.applet.Applet {  // la clase  principal del hilo
    private int cont; // contador
    private TextField t; // el texto
    private boolean puedeConsumir;
    private Productor productor;  // productor
    private Consumidor consumidor; // consumidor
   
    public void init() {  
        add(t = new TextField(10));
        t.setEditable(false);      
    }

    public void start() {      // comienzo a iniciar el hilo
        productor = new Productor(this);
        consumidor = new Consumidor(this);
        productor.start();
        consumidor.start();
    }
   
    public void stop() {      //parar el hilo
        productor.detener();
        consumidor.detener();
    }
   
    public synchronized void produce() {   // sincrinizacion del productor
        t.setText(String.valueOf(++cont));       
        notify();
    }
   
    public synchronized void consume() {   // sincronizacion del consumidor
        if(cont <= 0) {
            try {
                wait();
            } catch(InterruptedException e) {}
        }
        t.setText(String.valueOf(--cont));          
    }
}

class ProductorConsumidor // clase del productor  
    extends Thread
{
    protected boolean corriendo;                 
    protected P1 p1;
   
    public ProductorConsumidor(P1 p1) {
        this.p1 = p1;       
    }

    public void detener() {
        corriendo = false;
    }
}

class Productor  extends ProductorConsumidor { // la clase consumidor
    public Productor(P1 p1) {
        super(p1);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p1.produce();
            try {
                sleep((int)(Math.random()*1000)); // donde se duerme el hilo
            } catch(InterruptedException e) {}
        }
    }
}

class Consumidor extends ProductorConsumidor {
    public Consumidor(P1 p1) {
        super(p1);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p1.consume();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

///////////////////////////////////////////////////////////////////////////
package Hilo2;
import consola.ES;

public class P2{
    private int cont; // contador
    private int t; // el texto
   
    private Productor productor;  // productor
    private Consumidor consumidor; // consumidor
   
    public static void main(String[] args)
       
 
    {
        new P2();
    }
    public P2()
    {  
         productor = new Productor(this);
         consumidor = new Consumidor(this);
         productor.start();
         consumidor.start();
    }
  
    public synchronized void produce() {
        cont++;
        ES.escribe(cont+"\n");
        notify();
    }
   
    public synchronized void consume() {
        cont--;
        if(cont <= 0) {
            try {
                wait();
            } catch(InterruptedException e) {}
        }
        ES.escribe(cont+"\n");
    }
}

class ProductorConsumidor
    extends Thread
{
    protected boolean corriendo;                 
    protected P2 p2;
   
    public ProductorConsumidor(P2 p2) {
        this.p2 = p2;       
    }

    public void detener() {
        corriendo = false;
    }
}

class Productor  extends ProductorConsumidor {
    public Productor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.produce();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

class Consumidor extends ProductorConsumidor {
    public Consumidor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.consume();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}


////////////////////////////////////////////////////////////////////////////////
/*
 * P2a.java
 *
 * Created on 27 de junio de 2005, 06:49 PM
 */

package lab8;

/**
 *
 * @author  net201b
 */
public class P2a {
   
    /** Creates a new instance of P2a */
    public P2a() {
    }
   
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
    }
   
}

 


/*public static void main(){
        new P2();
    }
  
    public synchronized void produce() {
        cont++;
        ES.escribe(cont+"");
        notify();
    }
   
    public synchronized void consume() {
        cont--;
        if(cont <= 0) {
            try {
                wait();
            } catch(InterruptedException e) {}
        }
        ES.escribe(cont+"\n");
    }
}

class ProductorConsumidor
    extends Thread
{
    protected boolean corriendo;                 
    protected P2 p2;
   
    public ProductorConsumidor(P2 p2) {
        this.p2 = p2;       
    }

    public void detener() {
        corriendo = false;
    }
}

class Productor  extends ProductorConsumidor {
    public Productor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.produce();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

class Consumidor extends ProductorConsumidor {
    public Consumidor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.consume();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}
public class P2  { 
    private int contador;
  //  private boolean puedeConsumir;
    private Productor prod; 
    private Consumidor cons;
   
  public static void main(String[] args) 
  {
        new P2(); 
  }
 
  
   public void P2()
    {
        prod = new Productor(this);
        cons = new Consumidor(this);
        prod.start();
        cons.start();
    }
   /* public void stop()  
    {
        prod.detener();
        cons.detener();
    }
*/
    public synchronized  void produce()
    {
        contador++;
        ES.escribe(contador+"");
        notify();
    } 
    public synchronized void consume()
   
    {
        contador--;
        if(contador == 0) {
            try {
                wait();
            } catch(InterruptedException e) {}
        }
        ES.escribe(contador+"\n");    
    }
}

 

class Productor 
    extends ProductorConsumidor
{
    public Productor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.produce();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

class Consumidor
    extends ProductorConsumidor
   
{
    public Consumidor(P2 p2) {
        super(p2);
    }   
    public void run() {
        corriendo = true;       
        while(corriendo) {
            p2.consume();
            try {
                sleep((int)(Math.random()*1000));
            } catch(InterruptedException e) {}
        }
    }
}

class ProductorConsumidor
    extends Thread
{
    protected boolean corriendo;                 
    protected P2 p2;
   
    public ProductorConsumidor(P2 p2) {
        this.p2 = p2;       
    }

    public void detener() {
        corriendo = false;
    }
}