• 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

Threads

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I just need to get a few loose ends tied up here.
1. public void run() has to appear like this in code which implements Runnable.It's signature must stay the same and with no args added to the param. Is this correct?
2. Do the same rules apply to code that extends Thread as well, or is it OK if we even don't include this method in the code - will it give compile time error?
3. I know that Runnable is an abstract class and that the run() method is abstract, and can therefore understand that it must appear in code that implements Runnable. If you didn't incl run(), would that mean that you'd have to declare the class that implements Runnable as abstract?
4. Am I right in saying that Thread class already inherits from Runnable? If that's the case what implications does that have on Thread class?
The rest of the stuff on Threads I'm ok with. It's just some of these fundamental issues Iv'e got to get clear in my head.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. public void run() has to appear like this in code which implements Runnable.It's signature must stay the same and with no args added to the param. Is this correct?
correct
2. Do the same rules apply to code that extends Thread as well, or is it OK if we even don't include this method in the code - will it give compile time error?
Thread implements Runnable and provides an empty body for the run() method. If you don't override run() when subclassing Thread,your thread won't do anything.
There won't be any compilation error if you don't override the method run() since it is provided by the Thread class.
3. I know that Runnable is an abstract class and that the run() method is abstract, and can therefore understand that it must appear in code that implements Runnable. If you didn't incl run(), would that mean that you'd have to declare the class that implements Runnable as abstract?
Runnable is not an abstract class but an interface, both are different and that difference is very important.
If your class implements the Runnable interface and doesn't provide an implementation for the run() method then you have to declare your class abstract.
4. Am I right in saying that Thread class already inherits from Runnable? If that's the case what implications does that have on Thread class?
Again, the Thread class implements Runnable, this means that the Thread class provides an empty implementation for the run() method.
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Valentin for the clarrification
 
Ranch Hand
Posts: 93
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


1. public void run() has to appear like this in code which implements Runnable.It's signature must stay the same and with no args added to the param. Is this correct?


Thats correct.


2. Do the same rules apply to code that extends Thread as well, or is it OK if we even don't include this method in the code - will it give compile time error?


No,the same rules dont apply for the code that extens Thread,as thread as already a blank run method defined,and so the code which does not define a run method explicitly will not give compile time errors.


3. I know that Runnable is an abstract class and that the run() method is abstract, and can therefore understand that it must appear in code that implements Runnable. If you didn't incl run(), would that mean that you'd have to declare the class that implements Runnable as abstract?


First of all,runnable is an interface and not an abstract class.Rest everything is correct about your statement.


4. Am I right in saying that Thread class already inherits from Runnable? If that's the case what implications does that have on Thread class?


The thread class implements runnable.It does not have any particular implications,except that a thread class also can be used as another thread.
 
reply
    Bookmark Topic Watch Topic
  • New Topic