• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Confuon in Thread Problem

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Hi Ranchers,

I understand the process like ::
1.Create the Runnable Object ……
2.Create a Thread instance & give it your target Runnable created.
3.Now when we invoke start() on Thread instance it’ll invoke run method of target runnable.

But in above example as we created a Thread instance at (a),(b) & given it a Runnable created.When we invoked start method on different Threads created , (a) & (b) are calling
Their own (Thread subclass) methods instead of Target Runnble’s run() method (irrespective of line 1 or 2 is commented at a time).

Why they are calling thir own class’ run() method ???
Whereas if we use statement

Thread ob = new Thead(rd);
Ob.start();

Will call the run() method of target Runnable .In this case why not Thread class’ run() method called(which does nothing) ???

I am in big confusion please help me.

Thanks & Regards


(attempted formatting code)
[ October 03, 2006: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is from the Thread API.

run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread's run method doesn't do nothing. It calls run on the runnable it was passed at contsruction. If not passed a runnable it does nothing.

By overriding run() in MyThread1 and MyThread you lose this functionality.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
This is actually method overriding. Actually in Thread class the implementation given like

public void run() {
if (target != null) {
target.run(); // target is the Runnable object which we are passing
}
}


that is why it calls the super class(Runnable's )run method. Since you are overridden the method it doesnot calls the Runnbale's run method.. and did what you said ..

Please correct me if i am wrong..


 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Karthigeyan Kannabiran:
Hi,
This is actually method overriding. Actually in Thread class the implementation given like

that is why it calls the super class(Runnable's )run method. Since you are overridden the method it doesnot calls the Runnbale's run method.. and did what you said ..

Please correct me if i am wrong..


 
Maneessh saxena
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Keith Lynn:
This is from the Thread API.

run()
If this thread was constructed using a separate Runnable run object, then that Runnable object's run method is called; otherwise, this method does nothing and returns.



First of all thanks for your quick response.i do completely understang what you said but in this case I passed a Runnable to Thread subclass and its constructor passes it to Thread class
.ultimately what Thread class gets the target Runnable whose run() method
should be called.but why Thread sublass method is called and why not Runnable…..

Still confused ………. Please help
 
Tim LeMaster
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In your code above you subclass Thread and override the run method.



In the above code you make an instance of MyThread1 (with confusing names I might add, the instance of MyThread is named myThread1) then you call start(). After start() has done the work of creating a new thread it invokes run(). You have overridden run() so Thread.run() doesn't get called MyThread1.run() gets called instead. Thread.run() has the code to call run()on a runnable, if passed. Since this code is overridden your runnable run() method is never called.

Usually you subclass thread or implement runnable - not both at the same time.

Hope this helps.
 
Arch enemy? I mean, I don't like you, but I don't think you qualify as "arch enemy". Here, try this 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