can anyone explain:
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(); } }
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.
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.
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.
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.
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(); } }
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With