• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

can anybody tel me wat exactly Thread.start() will do?

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

here is wat i assume abt implementation of Thread.start() method,

1. when i create thread instance using
start method will invoke the Thread.run() which does nothing.

2. when i create thread instance using
start method will invoke runnable_instance.run()

correct me if i am wrong.
please explain abt other actions taken it start method


Thanks
 
Ranch Hand
Posts: 1704
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thread.start() start the thread and JVM internally calls the run() method of the thread.
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your understanding is correct. If you don't provide a runnable instance, then what you have to do is make a subclass of Thread and override run(). There's never a reason to create an instance of the Thread class itself without a Runnable.

Thread's other constructors -- the ones that don't accept a Runnable argument -- should have been made "protected", so that this was more obvious. But Thread is one of the very first classes from the original Java API, and as such, it has the kind of design issues you'd expect for a class written in a brand new language.
 
Raghu Arikeri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when a runnable object is passed while creating the instance of Thread class, how will it be decided whether to invoke run method of Thread class or that of the runnable instance???
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by RM Reddy:
when a runnable object is passed while creating the instance of Thread class, how will it be decided whether to invoke run method of Thread class or that of the runnable instance???



When the start() method is called, another thread will be created. And this new thread will call the run() method of the Thread class. This is why when you extend the Thread class, you need to override the run() method.

Now, if you don't extend the Thread class, or don't override the run() method, then obviously, the Thread class run() method will run. This run() method, checks to see if a runnable is available, which is set by the constructor. And if it is, it will call the runnable's run() method.

Henry
 
Raghu Arikeri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Henry Wong:


if a runnable is available, which is set by the constructor. And if it is, it will call the runnable's run() method.

Henry



In the following code there is a runnable set thru constructor, but it is the overriden run method of class X which is invoked.

As in the following code, when ther are 2 run methods (that is one implemented from runnable and the other overriden from thread class), how will the start method decide which run to invoke??! please explain me the logic how start method works in this scenario...



Thanks in advance
 
Henry Wong
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

As in the following code, when ther are 2 run methods (that is one implemented from runnable and the other overriden from thread class), how will the start method decide which run to invoke??! please explain me the logic how start method works in this scenario...



Okay, think of it this way...

The new thread calls the run() method of the Thread class... period. It does not "decide" which run() method to run. In fact, it does not even know there is another runnable method, being held as an instance variable, in the Thread class.

It calls the run() method of the Thread class... every time... so... in your example, it will print X.



Now... It is the default run() method, in the Thread class that routes the request to the runnable. And since you overridden it, it will be *not* be executed, and hence, Y will *not* be run.

Henry
 
Raghu Arikeri
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok
now i understood the concept.

thanks a lot friends
 
What is that? Is that a mongol hoarde? Can we fend them off with this tiny ad?
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic