views:

55

answers:

1

What is the Thread.State of a thread after Thread.yield() ? Is it a Thread.State.WAITING? Thanks.

+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
@krock: so it's Runnable though it gives the thread-scheduler an apportunity to activate a different thread?
Max
@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
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
Max
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
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
Max