JavaRush /Java Blog /Random EN /Level 25. Answers to interview questions on the level top...
zor07
Level 31
Санкт-Петербург

Level 25. Answers to interview questions on the level topic

Published in the Random EN group
Level 25. Answers to interview questions on the topic of level - 1
  1. What are all the states of the 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 synchronizedis not occupied by another thread. Otherwise, our thread will receive the BLOCKED state and will wait for the mutex object to be released.

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

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

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

    When a method is called wait(), the current thread releases the lock from the object 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 go into the BLOCKED state.

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

    Calling this method places the thread in 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 calling the method wait(), the current thread releases the lock from the object monitorand goes to sleep for 500 milliseconds. The object monitormay 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 go into the BLOCKED state.

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

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

    After monitor.wait(), the thread will go into the WAITING state. A method notify()called by another thread on an object monitorwill move 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 calling the method notifyAll()?

    notifyAll()"will stay" all the threads. One of all "sleeping" (WAITING) threads will go into the RUNNABLE state, take over the monitor of the object being used and continue its work. The rest will be in the BLOCKED state. As soon as the first “waking up” thread releases the monitor, which everyone else is waiting for, its fate will be repeated by the next thread (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 the synchronized block called wait()the mutex object. What state will these threads go to if the fourth thread calls notifyAll()?

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

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

    Despite the fact that and join(500)will wait(500)transfer the current thread to the TIMED_WAITING state, there are significant differences between them:
    join(500)called on a thread, wait(500)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 complete.
    When called, wait(500)the current thread will release the lock from the synchronized object and go to sleep for 500 milliseconds.
    After 500 milliseconds, in both cases the threads will continue to work.

  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 its work.
    When called, wait(500)the current thread will release the lock from the synchronized object and go to sleep for 500 milliseconds.

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

    When a method is called yield(), the current thread “skips its turn” and java immediately switches to executing 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