• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Thread question?

 
Ma JianHui
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which two cannot directly cause a thread to stop executing?

A.calling the yield method

B.calling the wait method on an object

C.calling the notify method on an object

D.calling the notifyAll method on an object

E.calling the start method on another thread object

Answer:A,E

I think the answer is wrong,because the method yield() cannot directly cause a thread to stop executing!
which methods can directly cause a thread to stop executing?
Thanks!!!
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15356
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

When you copy a question from a mock exam or book, we ask you to quote your sources. So, please tell us where you copied the question from.
 
David Grant
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the API:

public static void yield()

Causes the currently executing thread object to temporarily pause and allow other threads to execute.


So, as far as I can see, the answer to the question should be E only.
 
Sharma Tarun
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does not ask whether it stops temperorily or permanently. So, I thonk the answer is correct.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First to quote the source: Sun's SCJP1.5 e-Practice Exam

Even then notify doesn't cause a thread to stop.
I think the sense of the question is to ask about methods which stop an executing thread and out of those identify which directly cause the thread to stop.

As explained above notify and notifyAll don't cause a thread to stop (so no question of stopping it directly)

Now calling the wait method causes the thread to stop and wait (waiting from running)

The call to start on some thread or the call to yield doesn't mean a thread will be stopped. Its a matter chance which depends upon the current situation and thread scheduler.

As an example:
A thread is already running and start method is called on some other thread. We can't say that first thread will be stopped. It depends upon thread priority, thread scheduler etc.

Now calling the yield() method on a thread makes the thread to go to waiting state so that some other waiting thread may be given resources. But what if there is no other waiting thread?? The thread will continue to run.


Hope that makes things clear!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic