[solved]Mutithreading and anonymous classes and objects
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();
*/
in the above code , we are creating a new Thread by creating an anonymous
object of an anonymous class ( a subclass of the Runnable interface? )
but what when we pass this new Runnable object, it has ITS OWN run()
method overloaded.so the *new Thread object still does not have its run()
method overloaded.*the call by new Thread(....).start is to the run () of
the thread which is still not overridden!! am i getting it wrong , cause
this code works
No comments:
Post a Comment