Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • 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
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Query on threads ?  RSS feed

 
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i have something like this



here "Runnable" is target runnable object and MyThread class extends from Thread. when t.start() is called whether it will invoke run method of Thread class or the runnable passed in the constructor of MyThread and why ?
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thread t = new MyThread(Runnable) won't compile.
Maybe , you can define a class that implements Runnable interface and provid a run method for this class.
For example,


When t.start() is invoked, the thread will execute the run method defined in Work class.

Thread class itself implements Runnable interface. You can define a run method for your MyThread class.
You can do this :
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gurpreet

In your case Literally writing Runnable in the statement
Thread t = new MyThread(Runnable) will give compiler error as Helen suggested as Runnable is an interface in java.lang package.
You need to change the name of the object passed in the constructor of MyThread and then calling start() will invoke the run method defined in the class of that object and remember that the class should be implementing Runnable interface..


Hope that helps!!!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:if i have something like this



here "Runnable" is target runnable object and MyThread class extends from Thread. when t.start() is called whether it will invoke run method of Thread class or the runnable passed in the constructor of MyThread and why ?



Hi gurpeet, Do you mean this kind of scenery accordingly following program?


If you wonna mean this, then when t.start() is called at line 24, then it will invoke run method of MyThread class. If you want to this start method will call MyRunnable's Run method, Then you have to call it yourself in run method of MyThread class according line 15. Only Then MyRunnable's Run will be called. If you pass a runnable instance to a Thread class accordingly line 10, when you call t.start() which in turns calls Runnable's Run Method, not Thread's Run method.
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

gurpeet singh wrote:

here "Runnable" is target runnable object and MyThread class extends from Thread. when t.start() is called whether it will invoke run method of Thread class or the runnable passed in the constructor of MyThread and why ?


well, here you are passing the Runnable object to Thread class and when you say t.start this calls the Thread's run method and it execute the run method of Runnable.
from Thread class


public void run() {
if (target != null) {
target.run();
}
}



target is nothing but Runnable you are delegating to Thread.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Download Free Java APIs to Work with Office Files and PDF
htttp://www.e-iceblue.com/free-apis.html
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!