Strength deadlocks in threads?

I want to know what a deadlock is in threads, because in many books I studied how to avoid a deadlock situation, I just want to know what is a deadlock and code example for this?

+5
source share
8 answers

A dead end is a situation where a parallel program cannot continue.

A thread waits for another thread, and another thread waits for the first thread to complete.

A commonly used real-life example is traffic flow.

alt text

No traffic can move until another queue moves.

You can find a good discussion of dead ends here .

Update: This is one of the Java examples that I found on the Internet (Oreilly book). There are comments there, so you can easily understand this.

The dining philosophy problem is another good example for understanding dead ends.

removed dead link to Imageshack

The detection of deadlocks and the prevention of deadlocks are two interrelated areas that can be useful in studying deadlocks.

+22
source

A dead end is when A waits on B and B waits on A.

So you can have in thread A:

while(B.incomplete()){ B.wait(); } A.complete = true; 

and have in stream B:

 while(A.incomplete()){ A.wait(); } B.complete = true; 
+6
source

Here is an example of a dead end that does not use wait . As long as you have synchronization, there is a chance of a dead end.

 public class Deadlock { static class Deadlocker { private Deadlocker other; public void setOther(Deadlocker other) { this.other = other; } synchronized void doSomethingWithOther() { try { Thread.sleep(1); } catch (InterruptedException e) { } other.doSomething(); } synchronized void doSomething() { } } public static void main(String[] args) { final Deadlocker d1 = new Deadlocker(); final Deadlocker d2 = new Deadlocker(); d1.setOther(d2); d2.setOther(d1); Thread t1 = new Thread() { public void run() { d1.doSomethingWithOther(); } }; Thread t2 = new Thread() { public void run() { d2.doSomethingWithOther(); } }; t1.start(); t2.start(); } } 

A deadlock occurs when t1 is in d1.doSomethingWithOther() (and therefore has a lock on d1 ), and t2 is in d2.doSomethingWithOther() (and therefore has a lock on d2 ). When each thread tries to call doSomething() on the object, lock is turned on for the other thread, they ended up stuck waiting for each other.

Note that deadlock does not necessarily include only two threads. You can have a loop of any size. Worse, as soon as a deadlock occurs, any other thread that tries to obtain a lock that the deadlock is already holding will eventually become actually blocked, even without being in a loop.

+2
source

The deadlock is caused by a resource conflict that cannot be resolved directly without any resource management (for example, a graph loop that depends on two resource locks).

One of the most common (and commonly used for illustration) deadlock scenarios is block inversion:

  • Consider an application with two critical resources (resA, resB) and two locks (lockA, lockB). Each resource is protected by a corresponding lock (resA => lockA, resB => lockB).
  • Two resources compete for resources, lock reserve A (and therefore resource A), and then pause to switch context) before it can reserve lockB. Thread B gains control, locks lock B, and then tries to reserve lockA. This causes the thread to pause, and control returns back to Thread A, which is waiting on lockB, which is held by Thread B.

In this case, you will have a deadlock due to the cyclical dependency between two threads of two competing resources (lockA and lockB), which cannot be resolved without separate intervention.

This can be trivially allowed either:

  • Ensuring that two locks are resolved in order (not the best choice)
  • Only one lock for each critical section at a time (i.e. lock A lock before trying to capture the lock)
+1
source

Imagine the following logical logic.

  • In Trick-22 novel, the fighter pilot had to be grounded due to insanity. He could prove that he was not crazy so he could fly again. But by asking, wanting to join the battle to endanger his life, he would demonstrate that he was crazy.

  • North Korea wants G7 to provide economic assistance before ending uranium refinement. The US and Japan say: "There is no way, because they will refuse after receiving assistance."

  • System reboot conflict.

    • The system will not be shut down until all user processes have been terminated.
    • Editor, the user process will not end if the editing has not been saved.
    • Editing cannot be saved if a USB drive is present because the editor was called from a USB drive.
    • The USB drive has been removed because of driver updates. The USB drive cannot be installed until the system shuts down and restarts.
  • Android Robot Has Basic Guidelines

    A robot cannot harm a person or, through inaction, allow a person to do harm.

    The robot must obey any orders given to it by humans, except in cases where such orders would be contrary to the First Directive.

    A robot must protect its existence if such protection does not contradict the First or Second Directive.

The human inhabitants of the base sent a robot to extract a radioactive power source. Without an energy source, the base will be closed, and the human colony will die. But the robot discovers that the power source is so powerful and unshielded that processing it can lead to malfunctions of the robot and become a threat to the human colony.

+1
source

A dead end is when two (or more) threads are waiting for another. Thread A cannot end until thread B does something, and thread B cannot finish until thread A does something else.

0
source

Block threads while waiting on each others to free some resources, but by doing this blocking wait, they do not release resources; other threads need to Unlock. Threads cannot progress until resources are released, but because they are not progress, resources will never be released, threads are blocked and thus a “dead end”.

A good article by Stephen Toub may help you a bit.

0
source
 DeadLock is a situation when first thread is waiting for second Thread, while Second Thread is waiting for first thread completion. 

See “Traffic blocking traffic” for a better understanding of the “Inadequate blocking” situation.

enter image description here

enter image description here

 **Java Code Demo** public class DeadLockDemo { public static Object object1 = new Object(); public static Object object2 = new Object(); private int index; public static void main(String[] a) { Thread t1 = new Thread1(); Thread t2 = new Thread2(); t1.start(); t2.start(); } private static class Thread1 extends Thread { public void run() { synchronized (object1) { 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 (object2) { System.out.println("Thread 2: Holding lock 1 & 2..."); } } } } private static class Thread2 extends Thread { public void run() { synchronized (object2) { 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 (object1) { System.out.println("Thread 2: Holding lock 2 & 1..."); } } } } } 

enter image description here

0
source

All Articles