• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Call to yield() and thread state?

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does a call to yield() guarantee a change of state from running to ready-to-run state regardless of whether there are other ready-to-run thread objects?
Another way of putting it is: is it safe to declare that yield() does not neccessarily put the calling thread on hold even for the minutest amount of time? - meaning no state transition occurs

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there are no other Threads waiting to run, your call to yield() should return immediately.
 
Enthuware Software Support
Posts: 4602
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill, I guess the call to yield() will return immediately even if there are other Threads waiting to run. I mean, I don't think yield() is a blocking call.
Also, a call to yield is ignored if there are no 'ready to run' thread of the same priority as the calling thread.
Also, I am not sure whether there is any guarantee that another thread (of same priority and ready to run) will indeed run if this thread calls yield(). Couldn't find any info. But I would not assume it because ultimately threads are scheduled by the OS and even if you call yield() it may be possible that the OS does not schedule the other thread.
Please comment.
-Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Try out the world's only WebCompiler!
www.jdiscuss.com
 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both for your responses.
I sense the contradiction to both statements to when a yield() is called and there are no ready-to-run Threads. My reading of Bill's response is that a state transition is guaranteed as a call to yield() returns immediately - even for the minutest amount. But, what Paul is saying is that a call to yield() may be ignored as yield() is not a blocking call and therefore state transition may not occur.
The broader question may also be - what are the guaranteed states and transitions? I once had a healthy debate w/ my Java instructor who insisted that under NORMAL execution a thread can move from non-runnable (waiting, sleeping or blocked) to dead state. My position was (according to my limited knowledge), the only 2 transitions to dead state is from running or ready-to-run states.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think about what you are saying:
"I guess the call to yield() will return immediately even if there are other Threads waiting to run. I mean, I don't think yield() is a blocking call."
How can it return immediately if another Thread starts running?
Of course the Thread executing the call to Thread.yield() has to stop if another Thread gets to run. Here is what the source code says: "Causes the currently executing thread object to temporarily pause and allow other threads to execute."
(Of course, yield is a native method, so who knows exactly what happens.)
Bill
 
Paul Anilprem
Enthuware Software Support
Posts: 4602
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I see the point now.
But in any case, answer to the original question, "Does a call to yield() guarantee a change of state from running to ready-to-run state regardless of whether there are other ready-to-run thread objects?" is No.
Even if there are ready to run threads, it is possible that call to yield is ignored. Reason: The priority of ready to run threads was not same as the thread calling yield.
HTH,
Paul.

------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Try out the world's only WebCompiler!
www.jdiscuss.com
 
James Baud
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again, Paul, Bill. I'm still not sure if the JQ+ comment here is accurate.
Question ID :952739433290
In which of the following cases a thread will definitely be alive but not be running?
Wrong answer: The thread calls yield()
JQ+ Comment: Note that, yield() does not neccessorily put the calling thread on hold.

------------------
~James Baud
He who asks, is a fool for five minutes;
but, he who does not ask, remains a fool forever. (Chinese proverb)
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That comment seems to sum it up as far as I can see. I would nominate:
Thread.sleep( nnn )
and wait()
As rendering a Thread runnable but not running.
(suspend() too, but that is deprecated)
Bill
------------------
author of:
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That means : yield may put a thread on hold or may not??
It depends on the thread scheduling algorthmn the JVM is using?
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends on two things:
1) are there any other ready-to-run Threads
2) will the JVM let the other Thread run - according to its priority/scheduling method.
 
Slime does not pay. Always keep your tiny ad dry.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic