File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Thread.join() on unstarted thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread.join() on unstarted thread" Watch "Thread.join() on unstarted thread" New topic
Author

Thread.join() on unstarted thread

Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The following program, adapted from a mock exam question, prints "Joining" and then "Joined".

My question: Why doesn't this hang forever after printing the word "Joining"? I've never started thread t, so trying to join it should wait indefinitely.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
From the source code:

Since isAlive() is false, join() does nothing.
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
you never started the Thread:
Thread t = new Thread();
...rest of code elided ...
t.start();
System.out.println( "Joining..." );
t.join();
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
I know that ... I even said it in my question.
Robert Paris
Ranch Hand

Joined: Jul 28, 2002
Posts: 585
whoops, sorry. You're right. To be less vague, join was made to simply return quietly if the Thread is not started. Since you did not start the thread, it simply does nothing (but now seeing the guy who posted the source code - is that allowed in the license - it's obvious in there)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread.join() on unstarted thread