Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java can someone please explain the keyword synchronized job and explain to me the code and how deadlock is hapenning public class Deadlock { static

Java can someone please explain the keyword synchronized job and explain to me the code and how deadlock is hapenning

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();

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

App Inventor

Authors: David Wolber, Hal Abelson

1st Edition

1449397484, 9781449397487

More Books

Students also viewed these Programming questions