나는 알퐁스가 두 스레드가 동기화 된 메서드에 동시에 액세스 할 수 있습니까?
로 출력을 얻고이 프로그램을 실행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();
}
}
: 가스통이 나에게 절을했습니다! 가스통 : Alphonse가 나에게 절을했습니다!
그래서 두 스레드가 동기화 된 메소드에 동시에 액세스 할 수 있습니까?
가능한 복제본 [쓰레드가 교착 상태에 빠지는 것을 방지하기 위해 노력하고 있습니다] (http://stackoverflow.com/questions/749641/trying-to-wrap-my-wee-brain-around-how-threads-deadlock) –