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

Discussion

Sample03 is run - what will be the output?

public class Sample03 extends Thread
{
    public void run()
    {
        System.out.println("cycle");
    }

    public static void main(String[] args) throws InterruptedException
    {
        Sample03 job = new Sample03();
        job.start();
        job.join();
        job.start();
    }
}

  • A.cycle, then the second start() is ignored
  • B.An IllegalThreadStateException before any output
  • C.cycle, then an IllegalThreadStateException
  • D.cycle printed twice

Answer: C

The first start() succeeds and the join guarantees its output appears. A thread may never be started twice and cannot be restarted once it has completed, so the second call throws IllegalThreadStateException in main. Running the work again needs a new Thread object.

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

Post your comment

Please log in or register to post a comment.