• 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: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In some mock exams I have noticed
thread is started by calling only start() and not Th.start()(where th is the thread instance) .I also tested with some examples this works if a class extends thread but not if class implements runnable.

Does that mean if class extends thread , we can just use start () method to start a thread

Thanks
Smitha
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If a class extends Thread, then any instance of that class is a Thread, making the start() method available with an implicit "this."
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marc,

I didnt get your point.I can understand if it is this.start() since this is currently excuting object which is the instance of thread.But how come
just start() works..??

Thanks
Smitha
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Smitha,
As Marc said any instance of that class will be a thread & will have an "implicit" reference 'this' to all instance members.
Its not mandatory in such a situation to use 'this' you can call directly as well. 'this' is generally used to differentiate between a local variable & the field which it hides or shadows.Therefore,this.start(); & start() will have the same meaning in this context.

Hope it helps !
Thanks
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smitha, it depends whether you observered the call to start() in an instance method of the class which extends a thread or in the main method.
I'm sure code you are referring is inside an instance method and this should make clear what marc is trying to explain.

It is always makes things easy, if you can post the code.
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the last two posters explained, in the context of an instance method (with an implicit "this" reference), the start method inherited from Thread can be called directly...

Does that make sense?
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Smitha Ballikar:
...this works if a class extends thread but not if class implements runnable...


Note that Runnable is an interface with a single method, public void run(). The Runnable interface does not contain a "start" method. So if an object's reference type is Runnable, then start() cannot be called on that object.

The Thread class implements Runnable, providing implementation for the run() method. In addition, Thread defines a start() method. So if an object's reference type is Thread, then both start() and run() are available as methods.

(Of course, calling run() directly will execute that method's body in the current thread, instead of as its own thread.)
 
reply
    Bookmark Topic Watch Topic
  • New Topic