• Post Reply Bookmark Topic Watch Topic
  • New Topic

t.join() in Sun's thread example

 
Ram Venkata
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was going thru the Threads tutorial on Sun's site
http://java.sun.com/docs/books/tutorial/essential/concurrency/simple.html

I dont understand the use of t.join() after the interrupt, could any one explain?

while (t.isAlive()) {
threadMessage("Still waiting...");
//Wait maximum of 1 second for MessageLoop thread to
//finish.
t.join(1000);
if (((System.currentTimeMillis() - startTime) > patience) &&
t.isAlive()) {
threadMessage("Tired of waiting!");
t.interrupt();
//Shouldn't be long now -- wait indefinitely
t.join();-> ???
}
Rgds
VenX
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch!

t.join() makes the current thread wait until t ends. The interrupt() call wakes t from sleep, and we want to make sure t exits its run() method before we do anything else. That should happen almost instantly, but we don't have any guarantees that our next statement won't run before that happens, so join() gives us that assurance.

The "wait indefinitely" comment may be confusing since we expect this to happen so quickly. It just refers to the choice of join() signature. This one does not have a time-out parameter.

How do you like that tutorial? We recommend it all the time for folks who are new to threads.
 
Ram Venkata
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan. I am liking the tutorial. Threads is taking more time than expected. But the tutorial is definite good read.

Thanks again for the response.

Regards,
Venx
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!