Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deadlock - how does it happen in this example?

Tags:

can anyone explain:

  1. why do we get a deadlock?
  2. how can Gaston enter function bow before Alphonse exit that function? (it should return from function bowBack() in order to exit the function bow() - or)?

This is the output I get - and then program is stuck!

Alphonse: Gaston has bowed to me!

Gaston: Alphonse has bowed to me!

public class Deadlock {     static class Friend {         private final String name;         public Friend(String name) {             this.name = name;         }         public String getName() {             return this.name;         }         public synchronized void bow(Friend bower) {             System.out.format("%s: %s"                 + "  has bowed to me!%n",                  this.name, bower.getName());             bower.bowBack(this);         }         public synchronized void bowBack(Friend bower) {             System.out.format("%s: %s"                 + " has bowed back to me!%n",                 this.name, bower.getName());         }     }       public static void main(String[] args) {         final Friend alphonse = new Friend("Alphonse");         final Friend gaston = new Friend("Gaston");          new Thread(new Runnable() {             public void run() { alphonse.bow(gaston); }         }).start();         new Thread(new Runnable() {             public void run() { gaston.bow(alphonse); }         }).start();     } }  
like image 557
Ohad Avatar asked Aug 09 '17 07:08

Ohad


People also ask

How does a deadlock happen?

Deadlock occurs when a set of processes are in a wait state, because each process is waiting for a resource that is held by some other waiting process. Therefore, all deadlocks involve conflicting resource needs by two or more processes.

What is the deadlock with example?

Deadlock is a situation where two or more processes are waiting for each other. For example, let us assume, we have two processes P1 and P2. Now, process P1 is holding the resource R1 and is waiting for the resource R2. At the same time, the process P2 is having the resource R2 and is waiting for the resource R1.

What is deadlock how it is happen how do you avoid it explain with example?

It means the CPU can't take acquired resources from any process forcefully even though that process is in a waiting state. If we can remove the no preemption and forcefully take resources from a waiting process, we can avoid the deadlock. This is an implementable logic to avoid deadlock.

What happens during deadlock?

A deadlock occurs when 2 processes are competing for exclusive access to a resource but is unable to obtain exclusive access to it because the other process is preventing it. This results in a standoff where neither process can proceed. The only way out of a deadlock is for one of the processes to be terminated.


2 Answers

The synchronized block / method is synchronized to this, that is the object instance the block / method is called. (For static "object instance" is to be replaced with "class instance".)

That is your 2 objects get synchronized to themselves, not a common object.

Try something like this:

public class Deadlock {    static class Friend {       private final String name;       public Friend(String name) {          this.name = name;       }       public String getName() {          return this.name;       }       public void bow(Friend bower) {          synchronized (getClass()) {             System.out.format("%s: %s  has bowed to me!%n", this.name, bower.getName());             bower.bowBack(this);          }       }       public void bowBack(Friend bower) {          synchronized (getClass()) {             System.out.format("%s: %s has bowed back to me!%n", this.name, bower.getName());          }       }    }    public static void main(String[] args) {       final Friend alphonse = new Friend("Alphonse");       final Friend gaston = new Friend("Gaston");       new Thread(new Runnable() {          public void run() { alphonse.bow(gaston); }       }).start();       new Thread(new Runnable() {          public void run() { gaston.bow(alphonse); }       }).start();    } } 
like image 115
Usagi Miyamoto Avatar answered Oct 12 '22 00:10

Usagi Miyamoto


Thread 1: alphonse instance gets locked from alphonse.bow(gaston); which prints a line and then calls gaston.bowBack() (but gaston is locked from Thread 2 due to synchronized bow() instance called on it below)

Thread 2: gaston instance gets locked from gaston.bow(alphonse); which prints a line and then calls alphonse.bowBack()(but alphonse is locked from Thread 1 due to synchronized bow() instance called on it)

So they're both waiting for the release and cannot exit bow() method, hence the Deadlock

like image 37
isah Avatar answered Oct 12 '22 00:10

isah