What is DeadLock and difference between DeadLock and Starvation?

What is DeadLock and difference between DeadLock and Starvation?

What is DeadLock and difference between DeadLock and Starvation?

What is DeadLock and difference between DeadLock and Starvation?
  • If two threads are waiting for each other forever such type of infinite waiting is called deadlock.
  • Synchronized is the only reason for deadlock situation hence while using synchronized keyword we have to take special care.
  • There is no resolution technique for deadlock but several prevention techniques are available.
  • Deadlock in java is a condition which occurs when two or more threads get blocked waiting for each other for an infinite period of time to release the resources(Locks) they hold. Deadlock is the common problem in multi-threaded programming which can completely stop the execution of an application. So, extra care needs to be taken while writing the multithreaded programs so that deadlock never occurs.
Example:--
package com.java4us;

public class DeadLock extends Thread {
    A a = new A();
    B b = new B();

    public void run() {
        try {
            b.d2(a);
        } catch (InterruptedException e) {
        }
    }

    public static void main(String args[]) throws InterruptedException {
        DeadLock d = new DeadLock();
        d.m1();
    }

    public void m1() {
        this.start();
        a.d1(b);
    }
}
package com.java4us;

public class A {

    public synchronized void d1(B b) {
        System.out.println("Thread 1 start execution of d1() method");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        System.out.println("Thread 1 trying to call B's last() method");
        b.last();
    }

    public synchronized void last() {
        System.out.println("Inside A class: last method");
    }
}
package com.java4us;

public class B {

    public synchronized void d2(A a) throws InterruptedException {
        System.out.println("Thread 2 start execution of d2() method");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
        }
        System.out.println("Thread 2 trying to call A's Last() method");
        a.last();
    }

    public synchronized void last() {
        System.out.println("Inside B, this is Last() method");
    }

}
  • In the above program if we remove at-least one synchronized keyword then the program won’t enter into DeadLock. Hence Synchronized keyword is the only reason for DeadLock situation. Due to this while using Synchronized keyword we have to take special care. 

DeadLock vs Starvation

  • Long waiting of a thread where waiting never ends is called DeadLock.
  • Whereas long waiting for a thread where waiting ends yet the certain point is called starvation for ex. Low priority thread has to wait until completing all high priority threads. It may be long waiting but ends yet certain point, which is nothing but starvation

Leave a Reply

Your email address will not be published. Required fields are marked *

*