public class ExampleTwo {
    private static int x = 0;
    private static Object y = new Object();
    public static void main(String args[]) throws InterruptedException {
	Runnable r1 = new Runnable () {
		public void run() { 
		    synchronized (y) {
			x = 1;
		    }
		}
	    };
	Runnable r2 = new Runnable () {
		public void run() { 
		    synchronized (y) {
			x = x + 1;
		    }
		}
	    };
	Thread t1 = new Thread(r1);
	Thread t2 = new Thread(r2);
	t1.start();
	t2.start();
	t1.join();
	t2.join();
	System.out.println("x = "+x);
    }
}