Java Reference
In-Depth Information
public void live() {
new Thread() {
public void run() {
while (true) {
eat();
play();
sleep();
}
}
}.start();
}
public static void main(String[] args) {
new Pet("Fido", "beef", "Woof").live();
}
}
Solution 79: It's a Dog's Life
The main method creates a Pet instance representing Fido and invokes its live method. The live
method, in turn, creates and starts a Thread that repeatedly executes the eat , play , and sleep
methods from the enclosing Pet instance. Forever. Each of those methods prints a single line, so
one would expect the program to print these three lines repeatedly:
Fido: Mmmmm, beef
Fido: Woof Woof
Fido: Zzzzzzz...
 
 
Search WWH ::




Custom Search