【发布时间】:2010-10-19 11:26:17
【问题描述】:
首先,here's a sample:
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();
}
}
我不明白如何阻塞发生。 main 函数启动两个线程,每个线程都开始自己的弓。
“同步”究竟会阻止什么?为同一个对象运行相同的函数(正如我最初认为的那样)?同一类的所有对象都具有相同的功能?同一个对象的所有同步函数?同一类的所有对象的所有同步函数?
在这里帮帮我。
【问题讨论】:
标签: java multithreading deadlock