xin
xin

Reputation: 309

How to understand this detailed multithreading and anonymous class related code?

public static void main(String[] args) {

    new Thread(new Runnable() {
        public void run() {
            System.out.println("hello");
        }
    }) 
    {
        public void run() {
            System.out.println("world");
        }
    }.start();
}

Simply I get "world" with this. Why it can be written in this manner? What is the first run() method for?

Upvotes: 0

Views: 100

Answers (4)

Slimu
Slimu

Reputation: 2371

You have overridden the run method which prints hello with the one which prints world. This happened because in your code you have extended the Thread class with an anonymous class which overrode the run method, therefore the Runnable parameter's run is never called. You can update the code to use super.run() to call the runnable method from the original class.

Here is your code with the changes:

    new Thread(new Runnable() {
        public void run() {
            System.out.println("hello");
        }
    }) {
        public void run() {
            super.run();
            System.out.println("world");
        }
    }.start();

Upvotes: 1

davidxxx
davidxxx

Reputation: 131466

The Thread class defines run() in this way :

@Override
public void run() {
    if (target != null) {
        target.run();
    }
}

It invokes the run() method of the Runnable instance (target field of Thread)if this field is not null.

So when you create an anonymous class of Thread that overrides this method in this way:

new Thread(...) {
    @Override
    public void run() {
        System.out.println("world");
    }
}

The Thread run() method of the anonymous instance doesn't rely any longer on the target field to run the Runnable instance.
It execute only this code :

System.out.println("world");

So the Runnable instance passed to the constructor of Thread is totally ignored by run():

new Runnable() {
    public void run() {
        System.out.println("hello");
    }
}

Upvotes: 1

Bohemian
Bohemian

Reputation: 425208

This (contrived) code passes an instance of an anonymous Runnable class to an instance of an anonymous Thread class that overrides its usual implementation of run().

It's becomes (a little) clearer if you refactor it:

// The runnable instance
Runnable runnable = new Runnable() {
    public void run() {
        System.out.println("hello");
    }
};

// Anonymous Thread class with a custom run() method
new Thread(runnable) {
    public void run() {
        System.out.println("world");
    }
}.start();

The Runnable passed to the Thread, which would be executed by the thread's start() method, is ignored because the Thread object is also an instance of an anonymous class with a custom implementation of its run() method, which is hard-coded to print "world".

Upvotes: 4

Eran
Eran

Reputation: 393956

This code creates an instance of an anonymous sub-class of Thread and then starts that thread.

That sub-class overrides Thread's run() method with a method that prints "world".

The first run() method is part of an anonymous implementation of the Runnable interface which is passed to the constructor of the Thread instance. That implementation is ignored, since your Thread implementation overrides run().

The Runnable instance passed to the thread constructor is only executed (when the thread starts) if the Thread's run() method is not overridden.

Upvotes: 1

Related Questions