• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on Threads

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ravi,
Line 4 works because Thread implements Runnable interface, so subclass of Thread (myThread) can be cast to Runnable.
HTH
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lin.
 
Ravi Anamalay
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chi,
Thanks - much appreciated.
 
Getting married means "We're in love, so let's tell the police!" - and invite this tiny ad to the wedding:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic