/*
 * scrittura e lettura di un messaggio implementato come una variabile statica
 *
 * il thread destinatario legge continuamente la variabile per vedere se ci
 * sono messaggi
 */

class Destinatario implements Runnable {
	@Override
	public void run() {
		while (Busy.messaggio == null) {
		}

		System.out.print("	messaggio ricevuto: ");
		System.out.println("\"" + Busy.messaggio + "\"");
		Busy.messaggio = null;
	}
}

class Mittente implements Runnable {
	@Override
	public void run() {
		try { Thread.sleep(1000); } catch (InterruptedException e) {}
		Busy.messaggio = "ciao";
		System.out.print("messaggio inviato");
	}
}

public class Busy {
	static String messaggio;

	public static void main(String[] args) throws InterruptedException {
		Destinatario d = new Destinatario();
		Mittente m = new Mittente();

		Thread td = new Thread(d);
		Thread tm = new Thread(m);

		Busy.messaggio = null;

		td.start();
		tm.start();

		td.join();
		tm.join();
	}
}
