• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt with Thread's run method

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
horri khaled
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sachin

thanks for your reply
can you explain more.
 
sachin verma
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
here overriding is also occurring
modify your code like this



now you will find your desired output
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
horri khaled
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thinks sachin;

Now it seen be clear
Again thinks for your time and patience
 
It is difficult to free fools from the chains they revere - Voltaire. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic