Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raghu Arikeri
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Kj Reddy
Ranch Hand
Posts: 1704
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread.start() start the thread and JVM internally calls the run() method of the thread.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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???
 
Henry Wong
author
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20833
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok
now i understood the concept.

thanks a lot friends
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic