wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ..Question about the Threads join() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "..Question about the Threads join() method" Watch "..Question about the Threads join() method" New topic
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");
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ..Question about the Threads join() method
 
Similar Threads
Should we call super.run()...
wait() and notifyAll()
interrupting a thread
Using static varaibles in Threads
notify() vs notifyall()