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

Discussion

Sample54 is run - what will be the output?

public class Sample54 extends Thread
{
    public void run()
    {
        try
        {
            Thread.sleep(5000);
        }
        catch (InterruptedException ignored)
        {
        }
    }

    public static void main(String[] args) throws InterruptedException
    {
        Sample54 sleeper = new Sample54();
        sleeper.start();
        Thread.sleep(200);
        Thread.State observed = sleeper.getState();
        sleeper.interrupt();
        System.out.println(observed);
    }
}

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

Answer: B

A thread inside a call that will give up after a stated interval reports TIMED_WAITING, which covers sleep, wait with a timeout and join with a timeout. Plain WAITING is for the untimed forms, and BLOCKED is reserved for waiting on a monitor lock.

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

Post your comment

Please log in or register to post a comment.