Página 1 de 1

Problema con Thread, synchronized, wait, notify

Publicado: 20 Jun 2012, 19:25
por MichBukana
hola foro tengo un problema les enseño el codigo.

Código: Seleccionar todo

public class waitYnotify implements Runnable{
   
    static int x=0;
   
    public static void main(String[] args){
        Runnable uno = new waitYnotify();
        new Thread(uno).start();
         Runnable dos = new waitYnotify();
        new Thread(dos).start();
    }

    @Override
    public void run() {
        lanzar();
    }
   
    public synchronized void lanzar(){
        try{
            if(x==0){
                x++;
                System.out.println("soy el primero y me voy a dormir");
                this.wait();
                System.out.println("soy el primero y me acabo de despertar");
               
            }else{
                System.out.println("soy el segundo");
                this.notify();
                System.out.println("soy el segundo y he aviso al primero");
            }
           
        }catch(InterruptedException e){
            System.out.println(e);
        }
    }
}


existen 4 println. solo se cumplen 3 hay esta el problema. lo que yo deseo es que imprima esto:

soy el primero y me voy a dormir
soy el segundo
soy el segundo y he aviso al primero
soy el primero y me acabo de despertar

pero se ve que uso mal el notify.

Salu2!

Re: Problema con Thread, synchronized, wait, notify

Publicado: 22 Jun 2012, 16:11
por adwind

Código: Seleccionar todo




public class PrubaMich {
    
    public PrubaMich(){
        Accion a=new Accion();
        a.setListo(true);
     PrubaMich.Primero p=new PrubaMich.Primero(a);
     PrubaMich.Segundo p2=new PrubaMich.Segundo(a);
     p.start();
     p2.start();
    }
 
       private class Segundo extends Thread{
    private Accion listo;
        public Segundo(Accion a){
        listo=a;
        }
        
        public void run(){
        synchronized (listo) {
           
   
            if(!listo.isListo()){
                System.out.println("soy el segundo");
                   synchronized(listo){
                        listo.notify();
                   }
                    
                    System.out.println("soy el segundo y he aviso al primero");
            }
            
        }
        }
    
    }
    
    
    private class Primero extends Thread{
    private Accion listo;
        public Primero(Accion a){
        listo=a;
        }
        
        public void run(){
        synchronized (listo) {

            if(listo.isListo()){
                System.out.println("soy el primero y me voy a dormir");
                    try {
                        listo.setListo(false);
                        listo.wait();
                          System.out.println("soy el primero y me acabo de despertar");
                    } catch (InterruptedException ex) {
                        
                    }
                    
                  
            }
            
        }
        }
    
    }
    
    private class Accion{
    private boolean listo;
    
    public synchronized boolean isListo(){
    return listo;
    }
    
    public synchronized void setListo(boolean  lis){
    listo=lis;
    }
    }
    
    public static void main(String[] args) {
       new PrubaMich();
    }
    
}


Re: Problema con Thread, synchronized, wait, notify

Publicado: 27 Jun 2012, 18:58
por MichBukana
Gracias Adwind pero lo que me gustaría saber es que hago mal de ahí por que no se despierta al hacer el notify?