What will or won't cause a thread to block (a question from a test)
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 threa开发者_开发问答d is active again
- join() blocks until the thread it's waiting for terminates.
Am I missing something here?
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.
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.
精彩评论