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

Discussion

Sample48 is run - what will be the output?

public class Sample48
{
    public static void main(String[] args) throws InterruptedException
    {
        Thread reporter = new Thread(new Runnable()
        {
            public void run() { System.out.print("work "); }
        });
        reporter.start();
        for (int attempt = 0; attempt < 3; attempt++)
        {
            reporter.join();
        }
        System.out.print("done");
    }
}

  • A.work work work done
  • B.work done, then an IllegalThreadStateException
  • C.The second join blocks for ever.
  • D.work done

Answer: D

join on a thread that has already died returns at once, so joining three times is harmless and does not run the body again. Only start() is restricted to a single call; join may be made by any number of threads any number of times.

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

Post your comment

Please log in or register to post a comment.