
/**********************************************************************/
/*                                                                    */ 
/*                        TD 1, Exercice 1                            */
/*                                                                    */
/**********************************************************************/

public class UnEntier {
    int val;
    
    public UnEntier (int x) {
	val = x;
    }
    
    public int intValue() {
	return val;
    }
    
    public void setValue(int x) {
	val = x;
    }
}


public class Proc1 extends Thread {

    UnEntier refI;

    Proc1(UnEntier ent) {
        refI = ent;
    }
    
    public void run() {
	try {
            System.out.print("\nP1 starts...");
	    sleep(100);
	    refI.setValue(42);
            System.out.print("\nP1 ends...");            
	} catch (InterruptedException e) { return ; }
    }
}


public class Proc2 extends Thread {

    UnEntier refI;

    Proc2(UnEntier ent) {
        refI = ent;
    }

    public void run() {
	try {
            System.out.print("\nP2 starts...");            
	    while (refI.intValue()!=42) { 
		System.out.print("\nP2 waits...") ; 
                sleep (5) ; 
            } ;
            System.out.print("\nP2 ends...");            
	} catch (InterruptedException e) { return ; }            
    }
}

        
public class Exo1 {

    public static void main(String[] args) {
        UnEntier i = new UnEntier(0);
    
	new Proc1(i).start() ;
	new Proc2(i).start() ;
    }
}
    
                        
        
        

    
