Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

using RUN() in Thread class  RSS feed

 
nagaraja swamy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi i have one doubt regarding the use of Thread class

if i am extending a Thread class and creating some new class., is it possible to write more than one RUN() method.,

if it is not possible,and if i want to write different implementation for each thread object i create., then what i need to do.

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot have more than one method with the same name and the same parameters inside one class. So no, you cannot create multiple public void run() methods in the same class. There would be no way to tell those methods apart, how would you expect Java to know which one to use?

Create multiple classes, each with their own run() method.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And just to add to what was already said, in the specific case of Thread, you will be better off implementing Runnable and passing it to created threads rather than extending Thread and overriding its run() method.
 
Tim Holloway
Saloon Keeper
Posts: 18800
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the technical language of computer science (java dialect), the way they say what Jesper just stated is that in a given class definition, no 2 methods can have the same "signature".

If you don't know what a Java object signature is, I recommend studying it and the notation that they use to describe signatures. It's not just interesting - a lot of Java APIs specify what signature an object, property, or method has to have in order to be acceptable.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nagaraja swamy wrote:is it possible to write more than one RUN() method

<nitpick>
The fact is that you don't want to write even one RUN() method, because the correct name is run().
</nitpick>

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!