aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt with Thread's run method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt with Thread Watch "Doubt with Thread New topic
Author

Doubt with Thread's run method

horri khaled
Ranch Hand

Joined: Mar 24, 2008
Posts: 36
Hi guys,
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
Subclasses of Thread should override this method.

From java API


The output of the above code is "Thread" which be different than what the API says, really I am confused and I need your help about this doubt.

thanks in advance.


SCJP 5
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
I would suggest you go and read inner classes first.
You will find the answer yourself
It is the very basic example of Anonymous inner class...nothing unusual


SCJP || SCWCD

horri khaled
Ranch Hand

Joined: Mar 24, 2008
Posts: 36
Hi sachin

thanks for your reply
can you explain more.
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
hi
here overriding is also occurring
modify your code like this



now you will find your desired output
Biju Gopinathan
Ranch Hand

Joined: Mar 27, 2006
Posts: 35
Hi HORRI Khaled,

If you override the run method in the thread class, it will not call the run method of the Runnable which is passed to the Thread. Instead it will call the threads own overridden run method.


SCJP 5
horri khaled
Ranch Hand

Joined: Mar 24, 2008
Posts: 36
Dear sachin

I still confused
Now let me see you other situation that show you that this code is not only a simple overriding problem.

1- remove r from the thread constructor

the output still be "Thread"
2- remove the run method

and the output is "Runnable"
3- Now remove the parameter and the run

the output is nothing

so if that is simply a problem of overriding I think that the 2 and 3 must be the same which is not.
Correct me if I am wrong.
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
When you pass "r" to Thread then Runnable's method should be called.
But it is also overridden in Thread's inner class. So it would be called just because simple overriding.

Now when you remove "r" that worked fine because don't forget that Thread class itself implements Runnable Interface.


In your second case as you know overridden method from Runnable's inner class will still be there and called

And in your third case there isn�t any public void run() method's implementation to be called

Remember Runnable is like a "Job To Do"
And Thread is like a "worker"

[ June 12, 2008: Message edited by: sachin verma ]
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
Subclasses of Thread should override this method.

Now I understand what are you talking about and where did you get that.
It is from API and is written for the method that is there in Thread class

This below implementation is taken right from API(Thread class)
public void run() {
if (target != null) {
target.run();
}
}


See ultimately you have to override the method that is there in Runnable but what happens when we don't override it.
It should give you an error because you didn't override the abstract method of Runnable but that does not happen either.Just because of the above implementation of Run method.That was called in your third case(case 3) and did nothing
Because It is simply not left to the programmer.But if you provide your own definition(implementation of run method that is going to override the Thread's one and ultimately Runnable's abstract run method

hope it will help you
sachin
[ June 12, 2008: Message edited by: sachin verma ]
horri khaled
Ranch Hand

Joined: Mar 24, 2008
Posts: 36
Thinks sachin;

Now it seen be clear
Again thinks for your time and patience
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt with Thread's run method