aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Threads" Watch "Question on Threads" New topic
Author

Question on Threads

Ravi Anamalay
Ranch Hand

Joined: Apr 15, 2003
Posts: 38
Hi All,
1. class MyThread extends Thread {
2.public static void main(String args[]) {
3.MyThread t = new MyThread();
4.Thread x = new Thread(t);
5.t.start();
6.}
7. }

Now this was taken from Kathy & Bert's book - and the code works. I don't understand line 4 though. I thought that Thread constructor only took combinations of Runnable, String, ThreadGroup or nothing.
How come this works with an instance of MyThread, a subclass of Thread ?
Thanks in advance.


with kind regards,<br />Ravi
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Ravi,
Line 4 works because Thread implements Runnable interface, so subclass of Thread (myThread) can be cast to Runnable.
HTH


not so smart guy still curious to learn new stuff every now and then
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Ravi,
since MyThread is a subclass of Thread
t.start() will work.But am also surprised at the line
Thread x = new Thread(x).
Looking for an answer.
Thanks,
Ramnath
Ramnath krishnamurthi
Ranch Hand

Joined: Jan 22, 2003
Posts: 56
Thanks Lin.
Ravi Anamalay
Ranch Hand

Joined: Apr 15, 2003
Posts: 38
Hi Chi,
So an implicit cast from Thread to Runnable has taken place ? How do we know when such an implicit cast takes place ? Thanks.
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
Ravi,
As I understand implicit cast is performed by compiler if the cast is a widening conversion/upcast along the inheritance hierarchy.
eg : child class cast to parent class or interface implemented by the parent class

Originally posted by Ravi Varman:
Hi Chi,
So an implicit cast from Thread to Runnable has taken place ? How do we know when such an implicit cast takes place ? Thanks.
Ravi Anamalay
Ranch Hand

Joined: Apr 15, 2003
Posts: 38
Chi,
Thanks - much appreciated.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Threads