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 does yield prevent thread execution? 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 "does yield prevent thread execution?" Watch "does yield prevent thread execution?" New topic

does yield prevent thread execution?

Pramila Chinguru
Ranch Hand

Joined: May 05, 2006
Posts: 54
Which of the following will definitely stop a thread from executing
1. wait
2. notify

Ans: 1 3 4 5

Does yield definitely stop executing? Incase there are no high priority threads in ready state, the same thread continues to run. how is yield a correct answer to this question?
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
Hi pramila:
Actually whenever there are equal priority thread then the running thread can volunteerally leave the running state. Hence, it stops its execution.But this behaviour of yield() method is not guarenteed...

I hope it helps...

[ June 23, 2006: Message edited by: Swapnil Trivedi ]

SCJP 5.0<br />-----------<br />"Help Ever && Hurt Never"
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
This is from the API.


public static void yield()

Causes the currently executing thread object to temporarily pause and allow other threads to execute.
Swapnil Trivedi
Ranch Hand

Joined: Jun 06, 2006
Posts: 106
I am sorry for my last post..But I too am confused about this yield() method. I have one Question from Enthuware database that says that
"yield() does not necessarily put the calling thread on hold."

The Question was:

In which of the following cases a thread will definitely be alive but not running??
1. The thread has called wait().
2. The thread has issued sleep()
3. The thread calls yield()
4. A higher priority thread comes in ready to run state.
5. The thread is trying to enter a synchronized block and the monitor is not free.

Answer are: 1, 2 and 5

According to this concept the yield() does not stop execution. But according to Pramila's Question yield() definitely stops execution.

Can anybody explain....Thanks

It is sorta covered in the JavaRanch Style Guide.
subject: does yield prevent thread execution?
Similar Threads
Threads doubt??
Thread Question