Machine-translated by Google. Questions are authored in English.
Top

Discussion

Sample55 is run - what will be the output?

public class Sample55
{
    static final Object GATE = new Object();

    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Thread(() ->
        {
            synchronized (GATE)
            {
                try { GATE.wait(); } catch (InterruptedException e) { }
            }
        });
        t.start();
        Thread.sleep(200);
        System.out.println(t.getState());
        synchronized (GATE) { GATE.notifyAll(); }
    }
}

  • A.BLOCKED
  • B.RUNNABLE
  • C.WAITING
  • D.TIMED_WAITING

Answer: C

An untimed wait puts the thread in the wait set indefinitely, which is reported as WAITING. It is not BLOCKED, because it is not competing for a lock at that moment; it released the monitor when it called wait and will contend for it again only once notified.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.