This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Which of the method causes the currently executing thread object to temporarily pause and allow other threads to execute???

sleep()
wait()
notify()
yield()
join().

Is yield() the correct answer??
Anil Mundra
Greenhorn

Joined: Nov 14, 2008
Posts: 21
hi

I think it is wait()

the yield is used to restart that one .....
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Abhi, please quote your sources.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
I dont remember the name of the book.(I had got it from the library). It was just a question with no options/ answers, so I thought of clearing my doubt.
Ashika Chhabda
Greenhorn

Joined: Dec 29, 2008
Posts: 3
Yield() and sleep() both will temporarily pause the current thread .
[ December 29, 2008: Message edited by: Ashika Chhabda ]
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
sleep(), wait() and join().


SCJP 6
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Thanks.punit, I missed join().
Pandey Gautam
Greenhorn

Joined: Nov 05, 2008
Posts: 14
Hi,
join() is only method which stop currently executing thread and allow other thread to execute.
wait() may stop thread forever if notify is not called or time is not passed as arguments to wait(long time).

yield() didn't guarantee of executing other thread, it just preempted the current executing thread from running state to runnable.

sleep() make current thread to stop for given time and let other thread to execute.

So from my view only join and sleep are correct answer.


SCJP6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
join() is only method which stop currently executing thread and allow other thread to execute.
wait() may stop thread forever if notify is not called or time is not passed as arguments to wait(long time).


If you say wait() may stop a thread for forever, then join() can also do that. Suppose you are joining for a thread, that is calling wait() in its run method and nobody notifies that thread.

Pandey Gautam
Greenhorn

Joined: Nov 05, 2008
Posts: 14
join() will stop current executing thread for sure in which it is called and start new thread which is calling join(). it is different thing if other thread is calling wait() or went to deadlock etc.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Originally posted by Gautam Pandey:
join() will stop current executing thread for sure in which it is called
and
start new thread which is calling join().
I am not able to understand this line.


If your thread calling join() on my thread, then your thread will stop executing and wait until my thread completes its work and die. If my thread does not complete its work, then your thread will wait for my thread forever.

If you are calling
main(){
...
t1.start();
t1.join();// here main thread is calling join() on t1 thread, so main thread will wait until t1 thread completes its work.
...
}
Pandey Gautam
Greenhorn

Joined: Nov 05, 2008
Posts: 14
Originally by Punit

main(){
...
t1.start();
t1.join();// here main thread is calling join() on t1 thread, so main thread will wait until t1 thread completes its work.
...

}


ya you are right punit and what the question is asking.


Original question is
Which of the method causes the currently executing thread object to temporarily pause and allow other threads to execute???

sleep()
wait()
notify()
yield()
join().


Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
wait() may stop thread forever if notify is not called or time is not passed as arguments to wait(long time).


I understand the question, you are saying here wait() may stop currently executing thread forever, so I am just showing you that join() could also stop currently executing thread forever.
I am not able to understand what are you showing in the question ?
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
The real exam would be very clear on the assumptions if any and would leave little room for ambiguity.
So as per me actually only one method qualifies for the answer without any ambiguity is:

sleep()
Pause is guaranteed for the duration which i guess is subject to the precision of the system timers and schedulers as mentioned in the java API docs.
Also the question asks for a *method* so assuming it's not a typo that method is sleep(). Please correct me if i am wrong.


thanks
Harvinder
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread
 
Similar Threads
yield() in synchronized code - a Dan Chisholm question
Programming violations
Yeild method
Will a thread continue to run after yield() for a while ?
[thread].....