Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Threads  RSS feed

 
ravuri vinod
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Can anybody please tell me in detail for the following question.
When we start a thread by using t.start(),how it knows that to execute run()method ?


Thanks In Advance
Vinod.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

How does it know? Because that's just what it does; not sure how else to say it. The code that spawns a new thread it written to invoke the Thread's own method named run() in the new thread; Thread.run() looks something like



so you can either override run(), or provide a Runnable object.
 
Mandar Khire
Ranch Hand
Posts: 575
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By reading java doc & using netbeans, I think/read, Every program import java.lang package which Provides classes that are fundamental to the design of the Java programming language.In java.lang package there is one interface called Runnable which description is "The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread."
The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run.
In this Interface Runnable there is method Run()
When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread.
The general contract of the method run is that it may take any action whatsoever.
when we write public class t extends Thread {
this extended Thread use for class t,comes from Thread.java , & in Thread.java following code is written





/* from this i started thinking*/ So when we call t.start() it goes to if condition call start0(), then in start0() we call target.run(), target is our class t.
therefore When we start a thread by using t.start(), it knows that to execute run()method.
[ July 14, 2008: Message edited by: Mandar Khire ]
 
ravuri vinod
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Mandar Khire

Thank you very much for your excellent reply.
Now i understood about that.

Thanks &Regards
Vinod
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!