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 join()? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "join()?" Watch "join()?" New topic
Author

join()?

Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Question ID :988390217171
Which of the following calls will not stop a thread from executing ?
Assume that the thread is already started.
Ans:
start()
interrupt()
notify()
wait()
t.join()
Ans: start()/interrupt()/notify()..
why not t.join()?..It will allow the user thread to execute..it doesnt stop the user thread???
does the question refer to the main thread's being stopped when join is called?
I donno..
some times i feel coding are lottt better than these context oriented english ones
Anybody?
Ragu
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Ragu,
if you have a look at the comment it says that t is another Thread. In the user thread if you invoke join on another Thread it may stop the user thread and thus t.join() does not guarantee that the user thread won't stop after the invocation of t.join().
HIH


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
I agree with you. I speak English very well yet find the wording of this question confusing. Does it mean "any Thread in general" or "the currently executing Thread?"
HOWEVER, if you look at the actual answers, they help you to make sense of the question. Supposedly, there are no "Trick Questions", so you should always be able to determine the actual intent of the question by the answers.

t.join() (assuming t is a Thread), causes the CURRENT thread to wait for Thread t to die.
I have to admit I don't know if this is a busy wait or if the current Thread actually sleeps, but it seems to me either way it's not executing any code until the other thread completes.
Since this call DOES cause a thread to stop executing, and the question asks which statements do NOT stop a thread, t.join() is not a correct answer.

Hope this helps!
Rob


Rob
SCJP 1.4
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
why interrupt cannot stop the executing thread?
victor
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Interrupting a thread just sends an interrupt event to that thread. It doesn't stop the thread. In fact, it usually wakes it up!
if a method running in thread T makes this call:
try {
sleep(Long.MAX_VALUE);//sleep forever (at least a very long time)
}
catch(InterruptedException ie)
{
System.out.println("We just got interrupted by another thread!");
}

and another thread calls T.interrupt(), it will send an interrupt exception to thread T.
Thus, interrupt() never causes a thread to stop running.
Rob
[ January 12, 2002: Message edited by: Rob Ross ]
[ January 12, 2002: Message edited by: Rob Ross ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: join()?