Deadlock

Deadlock
A deadlock is a situation in which two or more competing actions are each waiting for the other to finish, and thus neither ever does.

In a transactional database, a deadlock happens when two processes each within its own transaction updates two rows of information but in the opposite order. For example, process A updates row 1 then row 2 in the exact timeframe process B updates row 2 then row 1. Process A can't finish updating row 2 until process B is finished, but it cannot finish updating row 1 until process A finishes. No matter how much time is allowed to pass, this situation will never resolve itself and because of this database management systems will typically kill the transaction of the process that has done the least amount of work.
In an operating system, a deadlock is a situation which occurs when a process or thread enters a waiting state because a resource requested is being held by another waiting process, which in turn is waiting for another resource. If a process is unable to change its state indefinitely because the resources requested by it are being used by another waiting process, then the system is said to be in a deadlock.
Deadlock is a common problem in multiprocessing systems, parallel computing and distributed systems, where software and hardware locks are used to handle shared resources and implement process synchronization.
In telecommunication systems, deadlocks occur mainly due to lost or corrupt signals instead of resource contention.
Deadlock occurs when multiple threads need the same locks but obtain them in different order. A Java multithreaded program may suffer from the deadlock condition because the synchronized keyword causes the executing thread to block while waiting for the lock, or monitor, associated with the specified object. Here is an example:
public class TestThread {
public static Object Lock1 = new Object();
public static Object Lock2 = new Object();
public static void main(String args[]) {
ThreadDemo1 T1 = new ThreadDemo1();
ThreadDemo2 T2 = new ThreadDemo2();
T1.start();
T2.start();
}
private static class ThreadDemo1 extends Thread {
public void run() {
synchronized (Lock1) {
System.out.println("Thread 1: Holding lock 1...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 1: Waiting for lock 2...");
synchronized (Lock2) {
System.out.println("Thread 1: Holding lock 1 & 2...");
}
}
}
}
private static class ThreadDemo2 extends Thread {
public void run() {
synchronized (Lock2) {
System.out.println("Thread 2: Holding lock 2...");
try { Thread.sleep(10); }
catch (InterruptedException e) {}
System.out.println("Thread 2: Waiting for lock 1...");
synchronized (Lock1) {
System.out.println("Thread 2: Holding lock 1 & 2...");
}
}
}
}
}
Output: -
C:\java>javac TestThread.java
C:\java>java TestThread
Thread 1: Holding lock 1...
Thread 2: Holding lock 2...
Thread 1: Waiting for lock 2...
Thread 2: Waiting for lock 1...
Deadlocks are a challenging problem to correct as they result in data loss, are difficult to isolate, create unexpected problems, and are time consuming to fix. Modifying every section of software code in a large system that access the database to always lock resources in the same order when the order is inconsistent takes significant resources and testing to implement. That and the use of the strong word "dead" in front of lock are some of the reasons why deadlocks have a "this is a big problem" reputation.
Let's change the order of the lock and run the same program to see if still both the threads waits for each other:
public class TestThread {.
public static Object Lock1 = new Object();.
public static Object Lock2 = new Object();.
public static void main(String args[]) {.
ThreadDemo1 T1 = new ThreadDemo1();.
ThreadDemo2 T2 = new ThreadDemo2();.
T1.start();.
T2.start();.
}.
private static class ThreadDemo1 extends Thread {.
public void run() {.
synchronized (Lock1) {.
System.out.println("Thread 1: Holding lock 1...");.
try { Thread.sleep(10); }.
catch (InterruptedException e) {}.
System.out.println("Thread 1: Waiting for lock 2...");.
synchronized (Lock2) {.
System.out.println("Thread 1: Holding lock 1 & 2...");.
}.
}.
}.
}.
private static class ThreadDemo2 extends Thread {.
public void run() {.
synchronized (Lock1) {.
System.out.println("Thread 2: Holding lock 1...");.
try { Thread.sleep(10); }.
catch (InterruptedException e) {}.
System.out.println("Thread 2: Waiting for lock 2...");.
synchronized (Lock2) {.
System.out.println("Thread 2: Holding lock 1 & 2...");.
}.
}.
}.
} .
}.
C:\java>javac TestThread.java.
C:\java>java TestThread.
Thread 1: Holding lock 1....
Thread 1: Waiting for lock 2....
Thread 1: Holding lock 1 & 2....
Thread 2: Holding lock 1....
Thread 2: Waiting for lock 2....
Thread 2: Holding lock 1 & 2....
Above example has been shown just for making you the concept clear, but its a more complex concept and you should deep dive into it before you develop your applications to deal with deadlock situations.


Free Web Hosting