aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question" Watch "Thread Question" New topic
Author

Thread Question

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707


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 ]

cmbhatt
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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


apigee, a better way to API!
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
run() method definition of the Thread class:


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

-cmbhatt
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 25, 2007
Posts: 1638


This will execute MyRunnable.run().
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 25, 2007
Posts: 1638

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.
Vikalp Singh
Ranch Hand

Joined: Dec 29, 2002
Posts: 50
this is right explenation
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 25, 2007
Posts: 1638

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
Arad Chear
Ranch Hand

Joined: Jan 05, 2007
Posts: 98
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 25, 2007
Posts: 1638


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

Joined: Feb 28, 2007
Posts: 1707
I overlooked the following line you added to my latest code:


new MyThread().start();



Thanks,
cmbhatt
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638


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

Well, it was there from the start
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 25, 2007
Posts: 1638

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Question