aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes thread question in k&b's book for 1.5 (3) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "thread question in k&b Watch "thread question in k&b New topic
Author

thread question in k&b's book for 1.5 (3)

Anna Chu
Ranch Hand

Joined: May 18, 2006
Posts: 30
The 17th question in chapter 9 self test:
public class TwoThreads {
static Thread laurel, hardy;
public static void main(String[] args) {
laurel = new Thread() {
public void run() {
System.out.println("A");
try {
hardy.sleep(1000);
} catch (Exception e) {
System.out.println("B");
}
System.out.println("C");
}
};
hardy = new Thread() {
public void run() {
System.out.println("D");
try {
laurel.wait();
} catch (Exception e) {
System.out.println("E");
}
System.out.println("F");
}
};
laurel.start();
hardy.start();
}
}

I think the code cannot compile because "hardy.sleep(1000)" is not correct since sleep() is a static function of Thread class, it should not be invoked by an instance.
Surendra Kumar
Ranch Hand

Joined: Jul 04, 2006
Posts: 235
Static methods can be called using class name or an instance reference.
But the latter is not good practice.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: thread question in k&b's book for 1.5 (3)