Home / Java Programming / Threads :: Discussion

Discussion :: Threads

  1. Which three guarantee that a thread will leave the running state?

    1. yield()
    2. wait()
    3. notify()
    4. notifyAll()
    5. sleep(1000)
    6. aLiveThread.join()
    7. Thread.killThread()

  2. A.
    1, 2 and 4
    B.
    2, 5 and 6
    C.
    3, 4 and 7
    D.
    4, 5 and 7

    View Answer

    Workspace

    Answer : Option B

    Explanation :

    (2) is correct because wait() always causes the current thread to go into the object's wait pool.

    (5) is correct because sleep() will always pause the currently running thread for at least the duration specified in the sleep argument (unless an interrupted exception is thrown).

    (6) is correct because, assuming that the thread you're calling join() on is alive, the thread calling join() will immediately block until the thread you're calling join() on is no longer alive.

    (1) is wrong, but tempting. The yield() method is not guaranteed to cause a thread to leave the running state, although if there are runnable threads of the same priority as the currently running thread, then the current thread will probably leave the running state.

    (3) and (4) are incorrect because they don't cause the thread invoking them to leave the running state.

    (7) is wrong because there's no such method.


Be The First To Comment