zor07
Level 31
Санкт-Петербург

Level 25

Published in the Random EN group
Level 25
  1. What are the states of an object Thread?

    • NEW
    • RUNNABLE
    • BLOCKED
    • WAITING
    • TIMED_WAITING
    • TERMINATED
  2. What states can a thread go to when entering a block synchronized?

    • RUNNABLE
    • BLOCKED

    In RUNNABLE, if the block of code marked with synchronizedis not occupied by another thread. Otherwise, our thread will get the BLOCKED state and wait for the mutex object to be released.

  3. What state will the thread go to when the method is called wait()?

    Calling this method puts the thread into the WAITING state.
    The method wait()can only be called inside a block synchronizedon a mutex object that has been “locked (locked)” by the current thread, otherwise the method will throw an IllegalMonitorStateException .

    Object monitor = getMonitor();
    synchronized(monitor)
    {
     …
     monitor.wait();}

    When a method is called wait(), the current thread releases the lock on the monitorand enters the WAITING state, waiting for the method to be called monitor.notify()by monitor.notifyAll()another thread. As soon as this happens, the thread will wake up and if the monitor was not busy, it will grab it and continue working.
    If the monitor is occupied by another thread, the current thread will enter the BLOCKED state.

  4. What state will the thread go to when the method is called wait(500)?

    Calling this method puts the thread into the TIMED_WAITING state.
    By analogy with the method wait(), wait(timeout)it can only be called inside a block synchronizedon a mutex object that has been “locked (locked)” by the current thread.

    Object monitor = getMonitor();
    synchronized(monitor)
    {
     …
     monitor.wait(500);}

    When the method is called wait(), the current thread releases the lock on the object monitorand sleeps for 500 milliseconds. The object monitorcan be captured by another thread.
    After 500 milliseconds, the thread will wake up and if monitorit was not busy, it will grab it and continue working.
    If the monitor is occupied by another thread, the current thread will enter the BLOCKED state.

    What state will the thread go to when the method is called notify()?

    Object monitor = getMonitor();
    synchronized(monitor)
    {
     …
     monitor.wait();}

    After monitor.wait(), the thread will enter the WAITING state. A method notify()called by another thread on the object monitorwill transition the thread from the WAITING state to the RUNNABLE state unless the monitor object is captured by another thread, otherwise to the BLOCKED state.

  5. What state will the thread go to when the method is called notifyAll()?

    notifyAll()"wake up" all threads. One of all the WAITING threads will enter the RUNNABLE state, grab the monitor of the object being used, and continue its work. The rest will be in the BLOCKED state. As soon as the first "awakened" thread releases the monitor that all the others are waiting for, the next thread will repeat its fate (an arbitrary thread will go from the BLOCKED state to the RUNNABLE state). This will continue until all awakened threads leave the BLOCKED state.

  6. Three threads in a synchronized block called wait()on a mutex object. What state will these threads go to if the fourth thread calls notifyAll()?

    Two of them will go to the BLOCKED state, one to the RUNNABLE state

  7. How is it different join(500)from wait(500)?

    Despite the fact that and join(500)will wait(500)put the current thread into the TIMED_WAITING state, there are significant differences between them:
    join(500)it is called on a thread, wait(500)it is called inside a synchronized block on the object on which this block is synchronized.
    When called, join(500)the current thread will wait 500 milliseconds for the thread whose method join()was called to terminate.
    When called, wait(500)the current thread will release the lock on the synchronized object, and sleep for 500 milliseconds.
    After 500 milliseconds in both cases, the threads will continue to run.

  8. How is it different wait(500)from sleep(500)?

    sleep(500)called on a thread, wait(500)called inside a synchronized block on the object on which this block is synchronized.
    When called, sleep(500)the current thread will wait 500 milliseconds, then continue.
    When called, wait(500)the current thread will release the lock on the synchronized object, and sleep for 500 milliseconds.

  9. What state will the thread go to when the method is called yield()?

    When a method is called yield(), the current thread "skips its turn" and java immediately switches to the execution of the next thread. The thread goes from state runningto state ready. The running & ready states are substates of the RUNNABLE state.

PS Comments, additions, corrections, remarks are welcome =)
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION