//=================================================================================
// 2 threads that deadlock each other - by locking objects in opposite order
// You should always lock objets in the same order 
// DO NOT RUN THIS IN THE JAVA*TESTER
//==================================================================================
public class zDeadlock
{
    final static Object resource1 = "Resource 1";
    final static Object resource2 = "Resource 2";

    public static void main(String[] args)
    {
        Thread t1 = new zDeadlockT1();        	//instantiate a thread 
	Thread t2 = new zDeadlockT2();
		
        t1.start();                      	//make it eligible for the scheduler to run it
        t2.start();                         
    }
}

class zDeadlockT1 extends Thread
{
    public void run( )
    {
        synchronized(zDeadlock.resource1)
        {
            System.out.println( this.getName( ) + " - locked: Resource 1.");

            Thread.yield( );              //yield, sleep, wait or I/O
                                            
            synchronized(zDeadlock.resource2)
            {
            	System.out.println( this.getName( ) + " - locked: Resource 2.");
 	    }
        }
    }
}

class zDeadlockT2 extends Thread
{
    public void run( )
    {
        synchronized(zDeadlock.resource2)
        {
            System.out.println( this.getName( ) + " - locked: Resource 2.");

            Thread.yield( );              //yield, sleep, wait or I/O
                                            
            synchronized(zDeadlock.resource1)
            {
            	System.out.println( this.getName( ) + " - locked: Resource 1.");
	    }
        }
    }
}