This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes can thread constructor take a thread object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can thread constructor take a thread object " Watch "can thread constructor take a thread object " New topic
Author

can thread constructor take a thread object

Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
thanks u so much I just culdnt see that part.

Rajesh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
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:


There is no emoticon for what I am feeling!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can thread constructor take a thread object
 
Similar Threads
one more thread
Question on Threads
Thread question
Thread question - uCertify 6.0
For an object to be a target for a Thread, that object must be of type: