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();
}
}
/*
new Thread(new Runnable() {
public void run() { gaston.bow(alphonse); }
}).start();
*/
在上面的代码中,我们new Thread
通过创建一个匿名类(Runnable 接口的子类?)的匿名对象来创建一个
但是当我们传递这个新Runnable
对象时,它有它自己的run()方法重载。所以 *new Thread 对象仍然没有重载它的 run() 方法。* new Thread(....).start的调用是到仍然没有被覆盖的线程的run()!!我弄错了吗,因为这段代码有效