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

Discussion

Sample07 is run - what will be the output?

public class Sample07 extends Thread
{
    public void run() { }

    public static void main(String[] args) throws InterruptedException
    {
        Sample07 job = new Sample07();
        boolean before = job.isAlive();
        job.start();
        job.join();
        System.out.println(before + " " + job.isAlive());
    }
}

  • A.true false
  • B.true true
  • C.false false
  • D.false true

Answer: C

A thread is alive only between being started and dying. Before start() it is NEW and not alive, and once join() has returned it has finished, so it is not alive either. The value would be true only if sampled while the thread was running, which is not a moment this program can name.

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

Post your comment

Please log in or register to post a comment.