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

Discussion

Sample06 is run - what will be the output?

public class Sample06
{
    static final Runnable NOTHING = new Runnable()
    {
        public void run() { }
    };

    public static void main(String[] args) throws InterruptedException
    {
        Thread probe = new Thread(NOTHING);
        System.out.print(probe.getState());
        probe.start();
        probe.join();
        System.out.print(" " + probe.getState());
    }
}

  • A.NEW DEAD
  • B.NEW TERMINATED
  • C.NEW RUNNABLE
  • D.RUNNABLE TERMINATED

Answer: B

A constructed but unstarted thread is NEW, and after join() has returned the thread has certainly died, so it reports TERMINATED. Those are the two points in a thread's life where the state is predictable. There is no DEAD constant; that name belongs to older documentation.

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

Post your comment

Please log in or register to post a comment.