File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can thread constructor take a thread object

 
Rajesh Chandra
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
Pls refer to the code below-

class MyThread extends Thread{
public static void main(String st[]){
MyThread t=new MyThread();
Thread x=new Thread(t);//here the thread constructor takes a thread object but it has to be a Runnable object
x.start();
}
public void run(){
for(int i=0;i<3;++i){
System.out.println(i +" ");
}
}
}

why inspite of a thread object in place of runnable object doesnt give compile time error?

Thanks in advance
Rajesh
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajesh Chandra:
... why inspite of a thread object in place of runnable object doesnt give compile time error? ...

Thread implements Runnable. Therefore, a Thread object is a Runnable object.
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks u so much I just culdnt see that part.

Rajesh
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As discussed above, yes it works, but don't do this in your own program, because this is not the way you are supposed to use threads, and it only makes the code confusing.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing that the OP may not be aware of, but has been long discussed,
is that there is seldom a need to ever subclass java.lang.Thread. Almost
always, what you are actually doing in you code is implementing Runnable:

In fact, it seems a mistake that Thread itself was defined as implementing Runnable.

With this in mind, one's code would look like the following:
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albrechtsen:
One thing that the OP may not be aware of, but has been long discussed, is that there is seldom a need to ever subclass java.lang.Thread. Almost always, what you are actually doing in you code is implementing Runnable...

As Cay S. Horstmann and Gary Cornell say in Core Java 2: Volume II - Advanced Features...
...forming a subclass of the Thread class ... is no longer recommended. You should decouple the task that is to be run in parallel from the mechanism of running it.

[ October 17, 2005: Message edited by: marc weber ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic