This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

..Question about the Threads join() method

 
Joshua Kueck
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joshua Kueck
Ranch Hand
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic