aspose file tools*
The moose likes Threads and Synchronization and the fly likes can anybody tel me wat exactly  Thread.start() will do? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "can anybody tel me wat exactly  Thread.start() will do?" Watch "can anybody tel me wat exactly  Thread.start() will do?" New topic
Author

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

Raghu Arikeri
Greenhorn

Joined: Jan 09, 2006
Posts: 15
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


Raghu
"Work for a cause and not applause. Live to express and not to impress"
Kj Reddy
Ranch Hand

Joined: Sep 20, 2003
Posts: 1704
Thread.start() start the thread and JVM internally calls the run() method of the thread.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Raghu Arikeri
Greenhorn

Joined: Jan 09, 2006
Posts: 15
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
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raghu Arikeri
Greenhorn

Joined: Jan 09, 2006
Posts: 15
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
Sheriff

Joined: Sep 28, 2004
Posts: 18108
    
  39

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

Joined: Jan 09, 2006
Posts: 15
ok
now i understood the concept.

thanks a lot friends
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can anybody tel me wat exactly Thread.start() will do?
 
Similar Threads
A question for beginners
Thread, start(), run() confusion
Whizlabs Thread Question
problem with classes
run method