File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exam tomorrow, please help. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "exam tomorrow, please help." Watch "exam tomorrow, please help." New topic
Author

exam tomorrow, please help.

Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
I am not quite sure about how join() works. For example, if in threadA there is a statement as B.join(). If thread B haven't been to a runnable state when threadA encounter the join() method. Would threadA wait until B has run or Would threadA just ignore the join() since B is not running? Thanks. I am waiting for some opinion.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

[ June 13, 2003: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
The source code for join() is basically this
while (isAlive()) { wait(0); }
If the thread is not started, then the thread is not alive and join() returns without the current thread (calling thread) waiting. When does the thread become alive? Some time after the thread is started and *presumably* before the run() method is called.
t.start()
t.isAlive() == true
run(){}
Is the thread alive before the start method returns? Or could there be a small window of time after start() returns and before the run() method is called when the thread is not yet alive? None of the Java thread books I have mention such a case.
[ June 14, 2003: Message edited by: Marlene Miller ]
Yuan Ye
Ranch Hand

Joined: Mar 05, 2003
Posts: 172
Thank you very much.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I have one more comment on the question,
When does the thread become alive? Is there a window of vulnerability when we could issue a join() after calling start() and before the thread isAlive()?

Native code.

The implementation is in the JVM. So you and I cannot know for sure.
However, if you start() a thread, then immediately invoke join() and it returns because the thread never even became alive, that would not be a reliable join(). It�s beyond our control. I vote that this cannot happen.
[ June 14, 2003: Message edited by: Marlene Miller ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene
When does the thread become alive? Is there a window of vulnerability when we could issue a join() after calling start() and before the thread isAlive()?

I guess that this will not happen as the method start() is synchronized.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Thank you Anupam. What I should have said is...
Is there a window of vulnerability when we could issue a join() after *returning* from start() and before the thread isAlive()?
In other words, does the thread become alive inside of start()? Or does the thread become alive some time later, just before run() is invoked?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Marlene
This is what I think.

In other words, does the thread become alive inside of start()? Or does the thread become alive some time later, just before run() is invoked?

I guess that a thread becomes alive inside the start method because the Thread's class run() method does nothing. Though I am not sure but I guess that the run() method directly follows the start() method.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: exam tomorrow, please help.
 
Similar Threads
Erratic behaviour of threads in program
Thread Interaction
Confused about Thread synchronization
join vs wait/notify
wait and notify help needed