views:

119

answers:

2

I've had a test, and there was a question I lost some points on, because I wasn't able to answer it :

Which of the following is NOT a condition which can cause a thread to block :

  • Calling an objects's wait() method
  • Waiting for an I/O operation
  • Calling sleep()
  • Calling yield()
  • Calling join()

As far as I know, all of these are blocking calls :

  • wait() returns when an something calls notify(), blocks until then
  • If the thread is WAITING for an I/O operation then it's obviously blocked
  • sleep(), obviously, blocks until the time runs out, or something wakes up the thread
  • yield() "cancels the rest of the thread's timeslice" (lacking a better term), and returns only when the thread is active again
  • join() blocks until the thread it's waiting for terminates.

Am I missing something here?

+4  A: 

yield(). it does not block the thread, what I mean it does not not put the thread in the BLOCK state, but it put the thread in the state READY - so it is available to the scheduler choose it again to execute. try to thing about the states of a thread. all operations but yield put the thread in the BLOCK state.

darlinton
I was actually about to pick that one, but I didn't, thinking that, from a point of view of the thread, yield() looks like a blocking call : the thread is unable to continue before yield() finishes. I didn't think of it terms of the states a thread can be in. Thanks.
fingerprint211b
If there is only one thread then `yield` does absolutely nothing.
Roman
the point is exactly the thread state. one thing is interrupt the thread execution, other thing is to block the thread executions. but look at the state and you find the answer.
darlinton
+1  A: 

Also, calling join() on a thread that has already finished will NOT block. And sleep(0) will behave like yield() if there are no other threads that are eligible for execution.

Stephen C