Menu
Topics Index
...
`


Multithreaded Programming >
Siva Nookala - 20 Feb 2017
Deadlock describes a situation where two or more threads are blocked forever, waiting for each other.

Example Program :
DeadLockDemo
class DeadLockDemo
{
    public static void main(String[] args)
    {
        Resource r1 = new Resource(); // LINE A
        Resource r2 = new Resource();
        UserOne thread1 = new UserOne(r1, r2); // LINE B
        thread1.start();
        UserTwo thread2 = new UserTwo(r1, r2); // LINE C
        thread2.start();
    }
}

class Resource
{

    void status()
    {
        System.out.println(Thread.currentThread().getName() + " is handling.");
    }
}

class UserOne extends Thread
{
    Resource r1;
    Resource r2;

    UserOne(Resource r1, Resource r2) // LINE D
    {
        this.r1 = r1;
        this.r2 = r2;
    }
    public void run()
    {
        synchronized (r1)
        {
            System.out.println(Thread.currentThread().getName() + "  is holding r1.");
            System.out.println(Thread.currentThread().getName() + "  is waiting for r2.");
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            synchronized (r2)
            {
                System.out.println(Thread.currentThread().getName() + "  is holding r1 & r2.");
            }
        }
        System.out.println(Thread.currentThread().getName() + " Leaving.....");
    }
}

class UserTwo extends Thread
{
    Resource r1;
    Resource r2;

    UserTwo(Resource r1, Resource r2)
    {
        this.r1 = r1;
        this.r2 = r2;
    }
    public void run() // LINE E
    {
        synchronized (r2)
        {
            System.out.println(Thread.currentThread().getName() + "  is holding r2.");
            System.out.println(Thread.currentThread().getName() + "  is waiting for r1.");
            try
            {
                Thread.sleep(100);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            synchronized (r1)
            {
                System.out.println(Thread.currentThread().getName() + "  is holding r2 & r1.");
            }
        }
    }
}
OUTPUT

Thread-1  is holding r2.
Thread-1  is waiting for r1.
Thread-0  is holding r1.
Thread-0  is waiting for r2.

DESCRIPTION

In the above program we have a Resource class and two users UserOne and UserTwo. At LINE A we have created two objects for Resource r1 and r2. At LINE B we created a thread for UserOne and started it by invoking thread's run method. At LINE C we created a thread for UserTwo and started it. At LINE D in inside UserOne run method we locked the r1 object. At LINE E we locked r2. Here thread1 need r2 to complete its function which is locked by thread2 and thread2 need r1 which is locked by thread1. thread1 will not unlock r1 until thread2 unlock r2 and thread2 will not unlock r2 until thread1 unlock r1. So there occurs a deadlock between thread1 and thread2.

THINGS TO TRY
  • To avoid deadlock in the above program we need to lock r1 instead of r2 in UserTwo run method so that there will be no lock for r2 until thread1 completes its execution.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.