// synchronized statements

import java.lang.*;
class T extends Thread {

	Object mutex;
	int myid;

	T (int id) {myid = id;}
	public void setMutex (Object o) {mutex = o;}

	public void run () {

		while (true) {
		   try { sleep(10); } catch (Exception e) {} 
		  // remove the above sleep, and it may result in starvation
		  // use yield() to solve startvation
		  // yield();
		  synchronized (mutex) {
			System.out.print ( myid + " is now the PM ");
			try {
				for (int i=0; i<10; i++) {
				   System.out.print(myid);
				   sleep(100);
			        }
			}catch(Exception e){};
			System.out.println ("\nPM" + myid + "'s turn over");
		  }
		}
	}
}

class Dummy {}

class SynchStatementDemo {

	public static void main (String args[]) {

		T t1 = new T (0);
		T t2 = new T (1);

		Dummy d = new Dummy();
		t1.setMutex (d);
		t2.setMutex (d);

		t1.start();
		t2.start();

	}

}

