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

Discussion

Sample10 is run - what will be the output?

public class Sample10
{
    public static void main(String[] args)
    {
        Thread.currentThread().interrupt();
        try
        {
            Thread.sleep(1000);
            System.out.println("slept");
        }
        catch (InterruptedException e)
        {
            System.out.println("caught " + Thread.currentThread().isInterrupted());
        }
    }
}

  • A.The program waits a second, then prints slept.
  • B.caught false
  • C.caught true
  • D.slept

Answer: B

The interrupt status is already set, so sleep throws InterruptedException immediately rather than waiting. Delivering that exception clears the status, so by the time the catch block runs isInterrupted reports false. Code that needs to pass the interruption on must call interrupt() again.

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

Post your comment

Please log in or register to post a comment.