这是我的示例线程代码。
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());
synchronized(bower) { //this is the change
bower.bowBack(bower);
}
}
public 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) throws InterruptedException {
final Friend alphonse =
new Friend("Alphonse");
final Friend gaston =
new Friend("Gaston");
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
// Thread.sleep(20);
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}
这会陷入僵局。 但如果我做了一点改变。我没有使用'bower'作为同步块的监视器,而是使用'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 synchronized void bow(Friend bower) {
System.out.format("%s: %s"
+ " has bowed to me!%n",
this.name, bower.getName());
synchronized(this) { //This is the change.
bower.bowBack(bower);
}
}
public 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) throws InterruptedException {
final Friend alphonse =
new Friend("Alphonse");
final Friend gaston =
new Friend("Gaston");
new Thread(new Runnable() {
public void run() { alphonse.bow(gaston); }
}).start();
// Thread.sleep(20);
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
}
}
请帮助我找到上述代码所示行为背后的正确理由。
答案 0 :(得分:1)
区别在于
synchronized(objectidentifier) {
// Access shared variables and other shared resources
}
这里,objectidentifier是对一个对象的引用,该对象的锁与synchronized语句所代表的监视器相关联。
synchronized(bower)使用来自凉亭的监视器,它是一个不同的对象。 你为凉亭做了一个锁。因为两个线程都锁定另一个线程中的对象,所以会发生死锁。
并且synchronized(this)使用自己的监视器。您为自己的对象锁定。线程将对象锁定在相同的线程中,无人问津。
如果我错了,请纠正我!
答案 1 :(得分:0)
你想要达到的目标是,在向某人鞠躬时,你不能退缩。
我会将synchronized
添加到bowBack
方法,并从synchronize(this)
synchronize(bower)
或bow...()