class processA implements Runnable 
{
    public void run() 
    {
        System.out.println("processA attempting to get Resource X");
        synchronized(ex5.resourceX) 
        { 
            try { Thread.sleep(1000); } catch ( InterruptedException e) { }
            System.out.println("processA attempting to get Resource Y");
            synchronized(ex5.resourceY) 
            {
                System.out.println("processA has X and Y - Begin!!!");
            }
        }
    }
}

class processB implements Runnable 
{
    public void run() 
    {
        System.out.println("processB attempting to get Resource Y");
        synchronized(ex5.resourceY) 
        { 
            try { Thread.sleep(1000); } catch ( InterruptedException e) { }
            System.out.println("processB attempting to get Resource X");
            synchronized(ex5.resourceX) 
            {
                System.out.println("processB has X and Y - Begin!!!");
            }
        }
    }
}

public class ex5 {
    static public Integer resourceX = 1;
    static public Integer resourceY = 2;
 
    public static void main(String[] args) 
    {
        //Start the threads
        Thread thread1 = new Thread(new processA(), "Process A");
        Thread thread2 = new Thread(new processB(), "Process B");
        thread1.start();
        thread2.start();
        try { Thread.sleep(10 * 1000); } catch ( InterruptedException e) { }
        System.out.println("Parent Thread - Killing processB ");
        thread2.stop();

    }
}
