• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Question

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Output:

MyThread
MyThread
MyRunnable

As I understand MyThread IS-A Thread so why doesn't it run the method of MyRunnable which is passed as "payload" to it to be run.

Please give me the reason!

Source of the program: Inspired from the Whizlabs


Regards,
cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
 
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The run() method of the Runnable, used to create a thread, is called from the run() method of the java.lang.Thread class. Since, you have overridden the run() method of Thread in your MyThread class and have not called super.run(), so there is no way the run() method of your runnable class will be executed. If you just want to execute the run method of the Runnable instance then you should not override the run() method in MyThread class.

Well, lot of run()'s, let me also just run away

P.S.: If you see the code of java.lang.Thread you will be further enlightened
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
run() method definition of the Thread class:


Thanks Nitesh: But I find it little obscure!
In other words...?

-cmbhatt
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew that what i wrote made little sense ... yeah sometimes i fail to explain clearly
Okie, so when you call start on a thread instance, after the JVM does all the stuff required to create a thread, it will call the run() method of the thread instance.
Now, in your implementation, MyThread, you have overridden the run() method. So, the JVM will call this run() method implemented by you instead of run() method in java.lang.Thread. Also, you have not called super.run() so the code in the parent class (java.lang.Thread)will never be called. agree?
Since, run() in the parent class is not called, so, no one is really aware of the Runnable instance passed to the constructor and hence it does not get executed.
However, when you directly instantiate java.lang.Thread using a Runnable, when this thread starts, the run() method in java.lang.Thread gets called and in turn it calls the run() method on the runnable instance initially passed(as you can see in the code.)
Hope it is clear
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Nitesh,


Also, you have not called super.run() so the code in the parent class (java.lang.Thread)will never be called. agree?



You are giving more emphasis to this super.run().
Could you please give me a little example when you call
super.run() and make my MyRunnable instance's run method
to be called when I do this :



Thanks,
cmbhatt
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This will execute MyRunnable.run().
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Nitesh,

Did you mean by the following way...



What I get is:

1- You created a class that extends Thread; Thread implements Runnable is known fact. I had overridden the run() method of the Thread class in the MyThread class.
2- When I called start() method on the MyThread instance (constructed using
MyRunnable), because I didn't call super.run() (in my previous post) to tell that Thread has to call the run() method of the Runnable interface that is passed to it (simply when you pass Runnable to the Thread constructor, you
are passing a Runnable implementor that overrides the Thread's run() method,
so therefore the Runnable's run() method is called.)

What when I didn't call super.run(),
Here I would have to disagree with Nitesh that Thread has no
knowledge of the Runnable

Thread has been constructed using MyRunnable, so it knows it.

Now my brain is fried...

Anybody come to my rescue!

Post Edited, wrong lines removed...

-cmbhatt
[ April 09, 2007: Message edited by: Chandra Bhatt ]
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

When you construct a Thread object using the constructor that takes
Runnable, because Runnable overrides the run() method, it's run method is
called.


No, its not because runnable overrides the run method(infact it does not override it defines.), it's run method is called. By constructing the Thread instance passing runnable you tell the instance to call the run on the runnable.
This will work only if you let the run() method of Thread execute, but, you are not allowing this method to execute as you have overridden it in your implementation i.e. MyThread.
This is the reason why, i suggested to see the implementation of java.lang.Thread.run(). I thought it will give you a clarity of the same. That method is the only place where the delegation to the passed runnable instance is done. If this method does not execute, the runnable run method will not be executed.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is right explenation
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole story came to my mind when I went through your following lines


This is the reason why, i suggested to see the implementation of java.lang.Thread.run(). I thought it will give you a clarity of the same. That method is the only place where the delegation to the passed runnable instance is done. If this method does not execute, the runnable run method will not be executed.




And specially If this method does not execute, the runnable run method will not be executed.

common

-cmbhatt
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chandra Bhatt:
The whole story came to my mind when I went through your following lines




And specially If this method does not execute, the runnable run method will not be executed.

common

-cmbhatt



I am glad that you understood the explanation. Enjoi
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is an error with your code :



there is no constructor such this in MyThread class
[ April 09, 2007: Message edited by: Arad Jack ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jack,


there is no constructor such this in MyThread class




It is not compiler error. I dont require MyThread() constructor
(default constructor), so I didn't define it.
I needed a constructor that require Runnable so I defined that.

What do you say?


Regards,
cmbhatt
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


It is not compiler error.


Who said so ?
It is a compiler error. You must define the no-arg constructor if you have defined any another constructor in the class. No-arg constructor will be added by the compiler only if there are no other constructors defined in the class.


I dont require MyThread() constructor
(default constructor), so I didn't define it.
I needed a constructor that require Runnable so I defined that.


But you were using it
[ April 09, 2007: Message edited by: Nitesh Kant ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I overlooked the following line you added to my latest code:


new MyThread().start();




Thanks,
cmbhatt
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


[QB]I overlooked the following line you added to my latest code:


Well, it was there from the start
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But meanwhile, we stopped using that line, it was not the main issue, main issue was


new MyThread(new MyRunnable());



Anyways to err is human. We should consider it typo. I think you are treating
is as blunder mistake.

---------------------------------------------------------------------------
"We lose to consider the the main concerns while entangled in the subtle
issues." Annonymous

Thanks,
cmbhatt
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frankly, i am not considering it as a blunder. I know to err is human and everyone makes mistakes big or small. So do I. In the last comment i was just pulling your leg that you probably felt bad about. Apologies about that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic