• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Query on threads ?

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic