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

Discussion

Sample14 is run - what will be the output?

public class Sample14 extends Thread
{
    Sample14()
    {
        super("loader");
    }

    public void run()
    {
        System.out.println(getName());
    }

    public static void main(String[] args) throws InterruptedException
    {
        new Sample14().start();
    }
}

  • A.loader-0
  • B.loader
  • C.Thread-0
  • D.main

Answer: B

Passing a name to Thread's constructor through super replaces the generated one, and getName inside run reports the thread that is executing, which is the worker itself. No counter is appended to a name that was supplied, so it is not loader-0.

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

Post your comment

Please log in or register to post a comment.