import java.io.*;
class enlace extends MyObject{
private ConditionVariable hidrogeno = null;
private ConditionVariable oxigeno = null;
private int contHidrogeno;
private int contOxigeno;
public enlace(){
hidrogeno = new ConditionVariable();
oxigeno = new ConditionVariable();
contHidrogeno = 0;
contOxigeno = 0;
}
public synchronized void hidro(){
contHidrogeno ++;
//System.out.println("Hola");
if(contHidrogeno == 2){
System.out.println("Se tiene 2 atomos de H");
notify(oxigeno);
wait(hidrogeno);
}
}
public synchronized void oxig(){
contOxigeno ++;
if(contOxigeno == 1){
System.out.println("Se tiene un atomo de O");
System.out.println("Se ha formado una molécula de aguan");
contOxigeno = 0;
contHidrogeno = 0;
notify(hidrogeno);
wait(oxigeno);
}
}
}
class H extends Thread{
private enlace mol;
public H(enlace x) { this.mol = x; }
//private int j=1;
public void run(){
for(int i=1;i<=20;i++){
mol.hidro();
/*try {sleep( (int)(Math.random()*10000) );}
catch(InterruptedException e){
System.out.println("Error:"+e.toString());}*/
/*try{ sleep(300);}
catch(InterruptedException e){}*/
/*if((i%2)==0){
mol.ponerAtomos(new Integer(j));
j++;
}*/
}
}
}
class O extends Thread{
private enlace mol;
public O(enlace x) {this.mol = x; }
int i=1;
public void run(){
while(i<=10){
mol.oxig();
i++;
/*try {sleep( (int)(Math.random()*10000) );}
catch(InterruptedException e){
System.out.println("Error:"+e.toString());}*/
/*try{ sleep(1000);}
catch(InterruptedException e){}*/
}
}
}
public class MoleculaAgua extends MyObject{
public static void main(String[] args){
enlace m=new enlace();
H hidrog = new H(m);
O oxige = new O(m);
hidrog.start();
oxige.start();
nap(1000);
System.exit(0);
}
}
|