Thursday, 10 August 2017

Deadlock Program


package rakesh;
class Deadlock1
{
    // Deadlock1 class to sleep a thread
    static void sleep(long millis)
    {
        try
        {
            Thread.sleep(millis);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}

// This class is shared by both threads
class Shared
{
    // first synchronized method
    synchronized void task1(Shared s2)
    {
        System.out.println("task1 started");
        Deadlock1.sleep(1000);

        // taking object lock of s2 enters
        // into test2 method
        s2.task2(this);
        System.out.println("task1 ended");
    }

    // second synchronized method
    synchronized void task2(Shared s1)
    {
        System.out.println("task2 started");
        Deadlock1.sleep(1000);

        // taking object lock of s1 enters
        // into task1 method
        s1.task1(this);
        System.out.println("task2 ended");
    }
}


class Thread1 extends Thread
{
    private Shared s1;
    private Shared s2;

    // constructor to initialize fields
    public Thread1(Shared s1, Shared s2)
    {
        this.s1 = s1;
        this.s2 = s2;
    }

    // run method to start a thread
    @Override
    public void run()
    {
        // taking object lock of s1 enters
        // into test1 method
        s1.task1(s2);
    }
}


class Thread2 extends Thread
{
    private Shared s1;
    private Shared s2;

    // constructor to initialize fields
    public Thread2(Shared s1, Shared s2)
    {
        this.s1 = s1;
        this.s2 = s2;
    }

    // run method to start a thread
    @Override
    public void run()
    {
        // taking object lock of s2
        // enters into test2 method
        s2.task2(s1);
    }
}


public class Deadlock
{
    public static void main(String[] args)
    {
        // creating one object
        Shared s1 = new Shared();

        // creating second object
        Shared s2 = new Shared();

        // creating first thread and starting it
        Thread1 t1 = new Thread1(s1, s2);
        t1.start();

        // creating second thread and starting it
        Thread2 t2 = new Thread2(s1, s2);
        t2.start();

        // sleeping main thread
        Deadlock1.sleep(2000);
    }
}






OUTPUT:



task1 started
task2 started

No comments:

Post a Comment