Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

thread state java

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:

  1. it starts
  2. it calls Thread.sleep (20ms)
  3. it calls getIn() method
  4. it tries to get a lock (lock.lock())
  5. if successfully gets the lock it calls Thread.sleep (100ms)
  6. if the lock is not available it calls waitingCond.await()
  7. after calling Thread.sleep (100ms) it calls lock.unlock()
  8. it calls another method getOut()
  9. it terminates (thread.join())

Given that, the following is my guessing about the thread state:

  1. READY TO RUN state
  2. TIMED WAITING state
  3. WAITING state
  4. WAITING state
  5. BLOCKED state
  6. WAITING state
  7. WAITING state
  8. TERMINATED state

Thanks

like image 284
qxc Avatar asked Jun 29 '12 16:06

qxc


People also ask

Do threads have states?

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.

What is thread running state?

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.

How many thread states are there?

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.


1 Answers

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:

  1. RUNNABLE
  2. TIMED WAITING
  3. RUNNABLE
  4. BLOCKED
  5. TIMED WAITING
  6. BLOCKED
  7. RUNNABLE
  8. TERMINATED

Thread State Transitions

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.

like image 130
Konrad Reiche Avatar answered Oct 02 '22 22:10

Konrad Reiche