| 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: 87
|
|
Static methods can be called using class name or an instance reference. But the latter is not good practice.
|
 |
 |
|
|
subject: thread question in k&b's book for 1.5 (3)
|
|
|