import java.util.Random;
import java.util.concurrent.locks.ReentrantLock;
import java.util.*;

public class deadlock {
    public   The_Store_1 a_store1=new The_Store_1();
    public   The_Store_2 a_store2=new The_Store_2();
    public static long start_time=System.currentTimeMillis()/1000;
    public static long now(){
        return((System.currentTimeMillis()/1000)-start_time);
    }

    public static void main(String[] args) {     
        new deadlock();
    }

    public deadlock(){
        long start_time=System.currentTimeMillis()/1000;
        Consumer_l c1 = new Consumer_l(a_store1,a_store2,1);
        Consumer_l c2 = new Consumer_l(a_store1,a_store2,2);
        c1.start();
        c2.start();
        try {
            c1.join();
            c2.join();

        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        System.out.println("All Consumers are Finished at " +deadlock.now()+".");
    }
}

class The_Store_1 extends ReentrantLock {
    public The_Store_1(){
    }
    public  int use(int i) {
        System.out.println("Consumer "+i+" Work Begun on Shared Resource 1 at " +deadlock.now()+".");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        System.out.println("Consumer "+i+" Work Finished on Shared Resource 1 at " +deadlock.now()+".");
        return 1;
    }


}
class The_Store_2 extends ReentrantLock {
    public The_Store_2(){
    }
    public  int use(int i) {
        System.out.println("Consumer "+i+" Work Begun on Shared Resource 2 at " +deadlock.now()+".");
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
        }
        System.out.println("Consumer "+i+" Work Finished on Shared Resource 2 at " +deadlock.now()+".");
        return 1;
    }
}

class Consumer_l extends Thread {
    The_Store_1 my_store1;
    The_Store_2 my_store2;
    int my_number;

    public Consumer_l(The_Store_1 a_store1,The_Store_2 a_store2,int i){  
        my_store1=a_store1; my_store2=a_store2;
        my_number=i;
    }
    public void run() {
        if (my_store1.tryLock()) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
            }
            while (!my_store2.tryLock()) {
                System.out.println("Consumer "+my_number+" got lock 1.\n    Could not get lock 2 at "+deadlock.now());                    
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                }
            }
        } else {
            if (my_store2.tryLock()) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                }
                while (!my_store1.tryLock()) {
                    System.out.println("Consumer "+my_number+" got lock 2.\n   Could not get lock 1 at "+deadlock.now());
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                    }
                }
            }
            my_store1.use(my_number); my_store2.use(my_number);
            my_store1.unlock();my_store2.unlock();
        }
    }
}