デッドロックの例は?
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 describes a situation where two or more threads are blocked forever, waiting for each other. Here's an example.
Alphonse and Gaston are friends, and great believers in courtesy. A strict rule of courtesy is that when you bow to a friend, you must remain bowed until your friend has a chance to return the bow. Unfortunately, this rule does not account for the possibility that two friends might bow to each other at the same time. This example application, Deadlock , models this possibility:
http://java.sun.com/docs/books/tutorial/essential/concurrency/deadlock.html
相手にお辞儀をした後、相手がお辞儀をする機会があるまでお辞儀をしなければなりません
印刷の説明を見ると、
a.bow(b):bがaにお辞儀をする
b.bowBack(a):aがbにお辞儀をする
もっと直感的に理解するには:
a.bow(b):aがbにお辞儀をする
b.bowBack(a):bがaにお辞儀をする