Forums Register Login

About Thread question?

+Pie Number of slices to send: Send
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!!!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
It does not ask whether it stops temperorily or permanently. So, I thonk the answer is correct.
+Pie Number of slices to send: Send
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!!
He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 760 times.
Similar Threads
thread problem
thread question
setPriority method on a thread object directly cause a thread to stop executing ?
a simple question
My Confusion About Thead
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 19:50:59.