5

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

7
  • Is there any moment where it is actually in the running state? :) Jun 29, 2012 at 16:20
  • @LukasKnuth You have disrupted OP's numbering where 5.1 was an optional subordinate step. You think it's better this way? Jun 29, 2012 at 16:22
  • That's pretty boring for the thread... And: You have not given us a question.
    – brimborium
    Jun 29, 2012 at 16:23
  • what do you mean by that? What i wrote down is the sequence of operations..shall I give you a few more details?
    – qxc
    Jun 29, 2012 at 16:24
  • 1
    @qxc No, but This is StackOverflow. The rules are: You ask a question, we try to answer it. You have not asked a question, but just given us some information and told us to intervene if we think your reasoning is incorrect. Have you tried this example? If so, did your expectation not match with your reasoning?
    – brimborium
    Jun 29, 2012 at 16:26

1 Answer 1

10

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.