package ex02;

public class EntierPartage 
{
	private int valeur = -1;
	private boolean modifiable = true;
	
	public EntierPartage()
	{
		
	}
	
	public synchronized void setValeur (int pVal)
	{
		while (!modifiable)
		{
			try
			{
				wait();
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		System.err.println(Thread.currentThread().getName() + " place " + pVal);
		this.valeur = pVal;
		modifiable = false;
		notify();
	}
	
	public synchronized int getValeur ()
	{
		while(modifiable)
		{
			try
			{
				wait();
			}
			catch(InterruptedException e)
			{
				e.printStackTrace();
			}
		}
		modifiable = true;
		notify();
		System.err.println(Thread.currentThread().getName() + " récupère " + this.valeur);
		return(this.valeur);
	}
}
