Forums Register Login

about run() method...

+Pie Number of slices to send: Send
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]
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
Try something like:

+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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..
+Pie Number of slices to send: Send
You're welcome!
+Pie Number of slices to send: Send
 

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...
+Pie Number of slices to send: Send
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]
+Pie Number of slices to send: Send
run method needs to be in the class which either implements Runnable or extends Thread
Warning! Way too comfortable! Do not sit! Try reading this tiny ad instead:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2393 times.
Similar Threads
Problem in Threading - 2
Thread's
Thread's
Thread's
Thread's
Threads
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 07:17:21.