• Post Reply Bookmark Topic Watch Topic
  • New Topic

why there is no argument thread constructor?  RSS feed

 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question may sound too naive, but the most stupid question is the one which is not asked.
So here it is..I wonder why there is a no argument constructor in Thread class, and there is a run method which does nothing.
is it the only use? i.e. to create subclasses, without explicitly calling the Super-class constructor. or is there more to it ?

 
amit punekar
Ranch Hand
Posts: 544
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
You can write the code that should be executed as part of the thread in the run() method.
The example that you have pasted below will save you from writing a complete Java class to do the same thing.
I believe this kind of construct is like Annonymous class where you instantiate the an Annonymous class and provide implementation for abstract method.

Regards,
amit
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The code that I have mentioned(as an anonymous class) is the very first thng that came to my mind, when I tried to find an answer.
My question still remains unanswered that, is this the only motive for non-argument constructor, in Thread class or there is something more to it?

Regards,
Rohit

 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why there is a no argument constructor in Thread class

Not sure how this is verified, but Thread class actually has a no arg constructor.
From the code of Thread class,

/**
* Allocates a new <code>Thread</code> object. This constructor has
* the same effect as <code>Thread(null, null,</code>
* gname<code>)</code>, where gname is
* a newly generated name. Automatically generated names are of the
* form <code>"Thread-"+</code>n, where n is an integer.
*
* @see #Thread(ThreadGroup, Runnable, String)
*/
public Thread() {
init(null, null, "Thread-" + nextThreadNum(), 0);
}



And the run method()
public void run() {
if (target != null) {
target.run();
}
}

The job of run() method is just to execute the run() of the runnable object passed to it in a new thread of execution. So, if there is no target runnable, then this thread is not supposed to execute anything and hence it will not do anything.
Hope this is clear.
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What I want to know is "the purpose" for having no-argument constructor in Thread class.
The code for run method and constructor is all fine.
I would like to know where one can "USE IT".

 
lokesh sree
Ranch Hand
Posts: 100
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry rohit.. misunderstood your question

One reason for the no-arg constructor could be to help in constructor chaining for sub-classes of Thread class.
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that constructor could have been protected, and you could still use it for sub classing. I'm with Rohit here, I don't see any value in having that constructor in its current form.
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob,

May be it is just a part of good coding practice, to provide a default no-arg constructor, but then why it is not protected?
 
Rob Spoor
Sheriff
Posts: 21095
85
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread stems from the old Java 1.0 days. There are quite a lot more oddities in that old code, some even worse than this (like java.util.Stack extending java.util.Vector, which allows for random access of the Stack elements through the inherited methods). Even though Sun created the language, its designers were just as perfect in their designs as anyone else - not perfect at all. My guess is that if Thread would have been created these days, this constructor would have been protected.
 
rohit chavan
Ranch Hand
Posts: 133
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Rob,

This one seems to be one of the very useful discussions I have had, till date on ranch.
Hope to see you around to help for more complex stuff.

Thanks,
Rohit
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!