简体   繁体   English

为什么我从此Java程序获取以下输出?

[英]Why i am getting following output from this java program?

class MyThread extends Thread 
{
    MyThread() 
    {
        System.out.print(" MyThread");
    }
    public void run() 
    {
        System.out.print(" bar");
    }
    public void run(String s) 
    {
        System.out.println(" baz");
    }
}
public class TestThreads 
{
    public static void main (String [] args) 
    {
        Thread t = new MyThread() 
        {
            public void run() 
            {
                System.out.println(" foo");
            }
        };
        t.start();
    }
}

Hello, I am new in java and currently learning Multithreading, When I am running the above program then i am getting this particular output MyThread foo please explain why? 您好,我是Java新手,目前正在学习多线程,当我运行上述程序时,我得到了这个特定的输出MyThread foo,请解释为什么?

MyThread comes from the constructor MyThread来自构造函数

foo comes from the run method which gets called when start() is called . foo来自run方法,该方法在调用start()被调用。

Essentially the run() method (which prints baz ) is overridden in your main . 本质上, run()方法(显示baz )在您的main覆盖

and the run(String s) is an overloaded method which has no significance here. run(String s)是一个重载方法,在这里没有意义。

It is executing the MyThread constructor and then executing the run() method . 它正在执行MyThread构造函数,然后执行run()方法。

Thread t = new MyThread() , you are trying to create a MyThread object and hence it executes the constructor. Thread t = new MyThread() ,您试图创建MyThread对象,因此它执行构造函数。

MyThread() 
{
    System.out.print(" MyThread");
}

Then you start the thread t.start(); 然后启动线程t.start(); , it executes the run() method which you have overridden in main(String[] args) : ,它将执行在main(String[] args)已覆盖的run()方法:

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

You first create an instance of MyThread , which calls the constructor and prints " MyThread". 首先,创建MyThread的实例,该实例调用构造函数并显示“ MyThread”。 Then, you call t.start(); 然后,您调用t.start(); which calls the run method. 调用run方法。

Even though you defined a run method printing " bar", it is overriden by the one in the main method (that prints " foo"). 即使您定义了一个run方法打印“ bar”,它也会被main方法(打印“ foo”)中的一个覆盖。

Try the following: 请尝试以下操作:

Thread t = new MyThread();
t.start();

And see what happens! 看看会发生什么!

The reason is that first when create object is invoked constructor MyThread and print first part of string. 原因是,首先在创建对象时调用构造函数MyThread并打印字符串的第一部分。 When you start thread, execution continue with run method and print foo . 当您启动线程时,将继续执行run方法并打印foo

The first output inside the default constructor of MyThread , and it overrides a run() method: MyThread的默认构造函数内部的第一个输出,它将覆盖 run()方法:

Thread t = new MyThread() 
        {
            public void run() 
            {
                System.out.println(" foo");
            }
        };

Output: 输出:

Thread foo 线程foo

Thread t = new MyThread() ;

Output: 输出:

MyThread bar MyThread栏

Note that first one you overrode the run() method and used your custom message inside it, but the second will override run() by default in MyThread class. 请注意,第一个覆盖了run()方法并在其中使用了自定义消息,但是第二个覆盖了MyThread类中的默认run()

You are overriding the run method when you create Thread t . 创建Thread t时,您将覆盖run方法。 This new implementation prints " foo", which takes priority over the one you implemented in your class MyThread which still runs its own constructor that prints "MyThread". 此新实现将打印“ foo”,它比您在类MyThread中实现的优先级更高,该类仍运行自己的构造函数,该构造函数将打印“ MyThread”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM