I'd like to check if my reasoning is correct.
First of all, I should provide a few details about the problem I'm trying to solve. A thread (part of a program) does the following things:
Given that, the following is my guessing about the thread state:
READY TO RUN
stateTIMED WAITING
stateWAITING
stateWAITING
stateBLOCKED
stateWAITING
stateWAITING
stateTERMINATED
stateThanks
A thread can be in any one of four states: New: The thread object has been created, but it hasn't been started yet, so it cannot run. Runnable: This means that a thread can be run when the time-slicing mechanism has CPU cycles available for the thread.
Life Cycle of a thread Runnable State: A thread that is ready to run is moved to a runnable state. In this state, a thread might actually be running or it might be ready to run at any instant of time. It is the responsibility of the thread scheduler to give the thread, time to run.
A thread is a path of execution in a program that goes through the following states of a thread. The five states are as follows: New. Runnable.
First of all, the state you describe with READY TO RUN
is actually RUNNABLE. For my bachelor thesis I had created a transition graph showing the different thread states and when they ought to change. You haven't described the semantics of getIn()
, so I guess it is just a random method.
If the thread is executing code, for instance on of your methods getIn()
or getOut()
it is RUNNABLE
and not WAITING
. BLOCKED
is actually only a very short transition state, which is always entered when a thread tries to claim a lock. If the lock is not available the thread keeps being blocked and cannot execute another action as you imply in step 6. Also it cannot invoke a method after calling Thread.sleep()
it has to wait, until the time is elapsed.
I would correct it the following way:
RUNNABLE
TIMED WAITING
RUNNABLE
BLOCKED
TIMED WAITING
BLOCKED
RUNNABLE
TERMINATED
Disclaimer: There is no guarantee for the transitions. It might even be, that a JVM vendor decides to implement the underlying mechanics in another way, for instance it could implementing blocking by spin-waiting.
If you want to dive deeper into this topic use a Profiler to find out the states of your thread. I have written one of my own to detect those states: Java Concurrency Profiler, but there are others out there as well, for instance VisualVM or YourKit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With