Sunday, September 22, 2013

What happens when we try to override run() method in any Thread's child class and call start() method and by passing a reference of Runnable object to this Thread Object?

Thread's run() and Runnable's run() together:

Hi All,
In this post I am trying to show you what happens when I try to run a thread by passing a Runnable object to it and also override run() method in it.

below is the code snippet:


A.java  

package com.nagarjuna.core.threads;

public class A extends Thread {

    A(Runnable r) {// constructor
        super(r);
    }

    @Override
    public void run() {
        System.out.println("this run is from extends thread class A");
    }
}//end class


B.java 

package com.nagarjuna.core.threads;

public class B implements Runnable {

    @Override
    public void run() {
        System.out.println("this run is from implements Runnable class B");
    }
}//end class


Test.java

package com.nagarjuna.core.threads;

public class Test {

    public static void main(String[] args) {

        B b = new B();
        A a = new A(b);
        a.start();
    }
}//end class


Suppose, if we try to execute the above code, what output we get.

we get the
 output:  this run is from extends thread class A




where the Runnable's run()'s statement has gone. Here the thread is neglecting the Runnable object 'b' and ignoring run() method. The thread is executing A's (Thread) run()'s statements.

If we see the source code in Thread class source code, We see below code inside of run() method.

code snippet:

public void run() {

        if (target != null) {
            target.run();
        }

    }//end run()

here the variable target is nothing but the passed Runnable object, here 'b' is runnable object.And code should execute the b's run() method.

Until here is ok, But still the thread is executing a's run().

It is because of Inheritance in java, we have overridden run() method of Thread class, It means that we are specifying that we are giving new feature to threads run(). So, The JVM calls the run() method of class A and neglects the run() code of class B.

If we want to run both the run() methods, simply add super.run() at the beginning or end of the run() method in class A.

code snippet:

A.java

package com.nagarjuna.core.threads;

public class A extends Thread {

    A(Runnable r) {// constructor
        super(r);
    }

    @Override
    public void run() {
        super.run();
        System.out.println("this run is from extends thread class A");
    }
}//end class

now we get the output like
output:  this run is from implements Runnable class B
                 this run is from extends thread class A



I think you got my point. Here, due to inheritance property in java we are unable to run both 'a' and 'b' run() methods. We can run both of these run() methods using the super keyword.


Please give your feedback on this post at nagarjuna.lingala@gmail.com or javaojavablog@googlegroups.com


source: http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Thread.java#Thread.run%28%29






No comments:

Post a Comment