| Author |
..Question about the Threads join() method
|
Joshua Kueck
Ranch Hand
Joined: Mar 14, 2002
Posts: 71
|
|
Shouldn't this print "Kids woke me up to early!" =========================================== public class MyThread implements Runnable { public void run() { try{ System.out.println("sleeping"); Thread.sleep(10000); } catch(InterruptedException e) {System.out.println("Restless"); } } public static void main(String args[]) { MyThread mythread = new MyThread(); System.out.println("Goodnight all"); try { Thread mine =new Thread(mythread); mine.start(); mine.join(7000); } catch(InterruptedException e) {System.out.println("Kids woke me up to early!");} System.out.println("Good Morning"); } } ================================================
|
 |
Matt Ghiold
Ranch Hand
Joined: Feb 24, 2002
Posts: 213
|
|
Actually, you never throw an interupted exception, so you won't see the kids woke me up early. Join, does not cause an InteruptedException to occur. You can do a <thread name>.interrupt(); and this will cause you to get the line you are expecting. Hope this helps.
|
-Matt<br /> SCJP2<br /> SCJD
|
 |
Joshua Kueck
Ranch Hand
Joined: Mar 14, 2002
Posts: 71
|
|
Java 2 exam cram must be wrong then. It says that " Other versions of join specify a maximum number or milliseconds to wait for the joined Thread to die. When that time expires, an Interrupted Exception is generated" what is the join(milliseconds) for then??
|
 |
Joshua Kueck
Ranch Hand
Joined: Mar 14, 2002
Posts: 71
|
|
The book is wrong, this prints out "not sleeping" last.. so join(ms) returns if it hasn't died yet..but throws no exception. public class MyThread implements Runnable { public void run() { try{ System.out.println("sleeping"); Thread.sleep(10000); System.out.println("not sleeping"); } catch(InterruptedException e) {System.out.println("Restless"); } } public static void main(String args[]) { MyThread mythread = new MyThread(); System.out.println("Goodnight all"); try { Thread mine =new Thread(mythread); mine.start(); mine.join(7000); } catch(InterruptedException e) {System.out.println("Kids woke me up to early!");} System.out.println("Good Morning"); } }
|
 |
 |
|
|
subject: ..Question about the Threads join() method
|
|
|