What is the Thread.State
of a thread after Thread.yield()
? Is it a Thread.State.WAITING
? Thanks.
views:
55answers:
1
+4
A:
No, the thread will still be in the RUNNABLE
state. Note that RUNNABLE
signifies that a thread is available to be run and may be either currently running or waiting its turn. Thread.STATE
does not distinguish between a thread that is currently executing and a thread that is ready to run, they are both RUNNABLE
.
A thread will only enter the WAITING
state when either wait()
, join()
or LockSupport.park()
has been called.
By calling Thread.yield()
method the currently running thread is voluntarily giving up its slice of CPU time. This thread then goes from running back into a ready state.
krock
2010-07-18 11:01:58
@krock: so it's Runnable though it gives the thread-scheduler an apportunity to activate a different thread?
Max
2010-07-18 11:19:07
@Max, yes `yield()` is a notification to the system that it should give other threads a chance to run. If there is nothing else to do, the thread could simply continue executing.
krock
2010-07-18 11:23:49
RUNNABLE, not RUNNING. It could run if the schedule gives it a slice. In contrast a thread calling join() can not run (ie is not runnable) at this time.
EricSchaefer
2010-07-18 11:29:11
Max
2010-07-18 11:32:07
Check this [this similar question](http://stackoverflow.com/questions/974277). I don't think it is really possible (without a ton of messing around), in a single CPU system your code running at the time is in the one and only executing thread.
krock
2010-07-18 12:00:56
You want to know if the thread is actually executing right now? I don't think you can figure this out from inside the jvm. You would also have to define what "now" actually means.
EricSchaefer
2010-07-18 12:00:58
Max
2010-07-18 12:03:03