• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I think it is wait()

the yield is used to restart that one .....
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yield() and sleep() both will temporarily pause the current thread .
[ December 29, 2008: Message edited by: Ashika Chhabda ]
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sleep(), wait() and join().
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.punit, I missed join().
 
Pandey Gautam
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic