This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes about run() method... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "about run() method..." Watch "about run() method..." New topic
Author

about run() method...

Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
I have two Questions..

Q 1) this program



i got problems... when i run this program it shows in the output only
>> In Main..
>>Thread is created...

program is not executing run() method..

then automatically terminated.... y??

Q 2) why "run()" method always in some other method or in other class.... why we cant write immediately below the "start()" method.. Please solve my doubts ...

thanking you...

[Edit - added code tags - MB]
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

Hi Amey. Welcome to The Ranch!

The reason it's not executing run is that you never tell it to. You never create a DemoThreadAlive object and pass it to the Thread. You're simply creating an empty Thread and starting that.

I don't understand your second question, sorry - you can add stuff below the start() call if you want it to execute in the main thread.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

How do you think the Thread object (referenced by t in your code) would know about a run() method in your class. What is the way to connect a Thread instance to a Runnable?
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

Try something like:

Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
Matthew Brown wrote:Hi Amey. Welcome to The Ranch!

The reason it's not executing run is that you never tell it to. You never create a DemoThreadAlive object and pass it to the Thread. You're simply creating an empty Thread and starting that.

I don't understand your second question, sorry - you can add stuff belo

Matthew Brown Thank you for your reply..

about your answer... Can't we create any thread without object...?? (..!!) but why..?? what is the cause.. I know whenever we create any class we should create its object or reference.. so is this answer that we can't create Thread without object...!!

about my Q2 (about run() method ) Can't we write it immediately below start() method...?? w the start() call if you want it to execute in the main thread.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Amey Ambulgekar wrote:about my Q2 (about run() method ) Can't we write it immediately below start() method...?? w the start() call if you want it to execute in the main thread.


Eh?

It's really not clear at all what you're asking. My best guess is that you're confused about the difference between start() and run().

run() is just another method like any other. You call it, it does its work, and it returns. However, there's never any good reason for you to call it directly.

start() is special. Thread.start() tells the JVM to kick off a new thread of execution, parallel to and independent of other threads that are currently executing. Once that new thread is established, it calls the run() method of the Runnable you passed to Thread's constructor.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

Amey Ambulgekar wrote:Can't we create any thread without object...?? (..!!) but why..??

Well yes, you can just create a Thread and run it. But it doesn't do anything.

As Jeff said, the effect of Thread.start() is to start a new thread of execution, and execute the run() method in that. But the default run() method in the Thread class does very little. All it does is call the run() method of the Runnable object that was passed in to the Thread constructor.

Which means that to get a Thread to do something useful, you need to do one of two things. You can extend Thread, and override the run() method. Or (usually preferable) you can create a Runnable object that does what you want, and pass that into the constructor. At the moment you aren't doing either. Your DemoThreadAlive implements Runnable, so you can create one of those and pass it in. But there's no magical way the Thread can know to run your run() method if you don't tell it.
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
Matthew Brown wrote:
Amey Ambulgekar wrote:Can't we create any thread without object...?? (..!!) but why..??

Well yes, you can just create a Thread and run it. But it doesn't do anything.

As Jeff said, the effect of Thread.start() is to start a new thread of execution, and execute the run() method in that. But the default run() method in the Thread class does very little. All it does is call the run() method of the Runnable object that was passed in to the Thread constructor.

Which means that to get a Thread to do something useful, you need to do one of two things. You can extend Thread, and override the run() method. Or (usually preferable) you can create a Runnable object that does what you want, and pass that into the constructor. At the moment you aren't doing either. Your DemoThreadAlive implements Runnable, so you can create one of those and pass it in. But there's no magical way the Thread can know to run your run() method if you don't tell it.


Well Matthew Brown i got it.... thank you...

Actually my java programming is started so little bit get confused that's why i asked this Q..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

You're welcome!
Amey Ambulgekar
Ranch Hand

Joined: Nov 22, 2011
Posts: 36
Matthew Brown wrote:You're welcome!


Sir..

back again for Q if we write whole program in one class(which have main method also..) for multi threading then where to write that run() method... means can we do like this .. for example



will it give error or exception....?? or WILL COMPILE AND RUN PROGRAM ...

Thanking you...
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4339
    
    7

Well, the best way to find out if something works is to try it yourself.

However, in this case you'll find that doesn't compile. You can't have one method declared inside another method, which is what you're doing there.

[I've added code tags to your post to make it more readable. You can do that with your own posts using the "Code" button in the editor toolbar - see UseCodeTags for more information]
Arun Kumar
Ranch Hand

Joined: Oct 10, 2007
Posts: 30
run method needs to be in the class which either implements Runnable or extends Thread
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about run() method...
 
Similar Threads
Thread's
Thread's
Thread's
Thread's
Problem in Threading - 2