• 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

Thread Question

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hallo there,

Quick question.

Can anyone explain why the code below would work (apologies if you've seen this before).

class MyThread extends Thread {

publis static void main (String [] args)
MyThread t = new MyThread();
Thread x = new Thread(t); //I expect this line to throw a compiler error
x.start(); //
}
....

The constructor above takea s a thread object as an argument which is not valid. Am I missing something?

Appreciate any pointers ....
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please check thread constructors in Thread API. You will get the answer.Thread API
Check the above link.
 
Ranch Hand
Posts: 358
Firefox Browser Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
java.lang.Thread class implements java.lang.Runnable interface
Again, by extending the above Thread class, your MyThread class too is implementing Runnable interface!
Thread class' constructors allow you to pass a Runnable object typically called "target". In this case, your MyThread object referenced by 't' is the target since it already implicitly implements Runnable interface!
Ont tip: Always treat Runnable objects as some "work"/"job" and Thread objects as "workers". So, workers are given job to do!
Thread t/*the worker*/ = new Thread(new RunnableImplementor()/*this is the job*/);
t.start();//asking the worker to start doing the work
 
Annette Sovereign
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for responding.

I think my question was not clear.

I expect the code I pasted to fail as the Thread object is taking a subclass of Thread as an argument when being created which is not valid. I expect compilation to fail but according to the book I got the snippet of code this answer is wrong.

My question is - should compilation fail or not?

As always, I'd appreciate an explanation (I've already had a look at the API and it appears to support my reasoning).


Thanks.
 
Annette Sovereign
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Okay - so I decided to have a word with the compiler myself and he/she/it did not complain. So I guessed I must be wrong and the book correct (why isn't there a humble pie smiley??).

Anyway, I think I've figured it out now and know where I went wrong (when answering the question, I reeled off the list of constructors in my head).

Thanks to those that responded.

Happy Studying
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic