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

Discussion

Sample84 is run - what will be the output?

public class Sample84 extends Thread
{
    private final String tag;

    Sample84(String tag)
    {
        this.tag = tag;
    }

    public void run()
    {
        System.out.print(tag + " ");
    }

    public static void main(String[] args) throws InterruptedException
    {
        Thread t = new Sample84("sub");
        t.start();
        t.join();
        System.out.print("main");
    }
}

  • A.main sub
  • B.main only
  • C.Either order
  • D.sub main

Answer: D

Correct answer: sub main

t.start() begins the subclass’s run, which prints sub, and t.join() blocks main until it has finished. Only then does main print. Without the join the order would be genuinely indeterminate, which is what the “either order” option describes - and it is exactly what join exists to prevent.

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

Post your comment

Please log in or register to post a comment.