• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Thread /Runnable

 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I create a class that extends Thread and implements Runnable.
And use these 2 classes for testing.
The output I get is below:

ShivaniThread
ShivaniThread





Can some one explain why this is coming .
And not -
ShivaniThread
ShivaniRunnable

{{** Constructors need to be written in the subclass since they are not inherited }}


Thanks,
Shivani.
 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I think this is because the run method of myRun class is overriden in myT class.

Actually when you pass the Runnable object to the super class it finds that the run method has been overriden by the myT class itself and hence it runs the method of myT class.

I know my answer is not precise, but i think it is going something like this.

Do correct me if I am worng.

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

In this case although you have passed a runnable object to a subclass of Thread which passes this object to the superclass Thread in the constructor but when you call t2.start() it finds the method run() in myT itself so it doesnt take a glance over the runnable object's run() method if you remove the run() method from myT class it is sure to call the run() method of the runnable object.

heres a code which does the same,




This compiles fine and prints,
--------------------------------------------------------------------------
ShivaniRunnable
--------------------------------------------------------------------------

i hope you know why "ShivaniThread" is not printed because now you dont have any run() method of myT class so Thread class run() method is called,

i think i have made myself clear.

Thanks,
With Luck,
Anand
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is unwise to override the start method of the Thread class in your subclasses of Thread. Anything an overridden start method does is done in the context of the calling thread until the "real" start method of the superclass Thread is called. I do wonder why Thread's start method is not final[b]. Anyone have some ideas as to why start is not [b]final?
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm,
the answer I was looking for was on this lines

"The Thread class start method calls the Runnable's run() method if the constructor invoked is Thread(Runnable obj)."

Any one has more details on the above?

Now given that I am overriding start() method - hence the run() of Runnable is not called. Yes that's understood.

Thanks,
Shivani
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shivani, do you realize that in your program you are not creating a new thread context? Your program is running completely in the context of the original main thread.
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This MIGHT be useful
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kedar I read through the link . Still a lot of things seem unclear to me.
Here I have added a super.start() in the overriden start() method.
Now this should ensure that whatever happened in the Thread class start method happens now as well.... Right? .Also , as Barry has pointed out,
doing this SHOULD ensure that a new thread is created and then the run is executed?.



The ouput i see is :
ShivaniThread
ShivaniThread

Whereas after adding the super.start() call I expected :
ShivaniThread
ShivaniRunnable

Kindly correct my incomplete understanding...

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

Here is the exact definition of the run method of Thread class where target is the Runnable object referred by object of Thread Class



now according to your code



the expected output should be
ShivaniThread
ShivaniRunnable


However
before that let us see what does the start method of the Thread class do.



Start method of Thread class causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread. The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its
run method).



Since you have overridden the run method so run method of MyT class is executing and there is no way for run method of Runnable to execute until you skip overridding run method of Thread class

that is why output is

ShivaniThread
ShivaniThread

and following post of Berry


Anyone have some ideas as to why start is not final?



By not start() being final, gives access to programmer to do some additional checks/tasks in the start method while super.start() will be necessary in that case.

plz correct me if i am wrong.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Arvind,
Thanks for the reply.I looked up the code in the Thread class.

start() was a native method with the signature as follows:



and run() method checked if an internal Runnable referance named target was not null.



Now when we do

Thread t1 = new Thread(objRunnable);

target is set to objRunnable.
when i call t1.start() if the overriden start gets called which calls super.start() as explicitly written in the code mentioned above. That creates a new thread. and calls the overriden run method .
If in the overriden run() method I add the line

super.run(); then it will invoke the run() of Thread class which will invoke the runnable's run() method that will print ShivaniRunnable.

To Conclude : ... We need to call both super.start and super.run while extending the Thread class , if we wish to use our own class implementing Runnable interface with it.

Thanks everybody.

Shivani
[ September 19, 2005: Message edited by: Shivani Chandna ]
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shivani,

thanx for correcting me

......super.run while extending the Thread class , if we wish to use our own class implementing Runnable interface with it.


However my emphasis was on showing the method calls.

thanx again
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend that you do only one of the following:
1. Extend Thread, override the run method, then call start on an instance of the extended class,

or

2. Pass an instance of Runnable to the constructor of a Thread instance. Then call start on the thread instance.

Do not mix the two methods.
 
Shivani Chandna
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Barry Gaunt:
Do not mix the two methods.[/b]



Yes Barry, Shall never do that on real projects.

In the above question , I was just trying to play around - to understand the functioning of the Thread class.

Cheers,
Shivani
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic