• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

ThreadGroup question

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I was trying to run the following code:



when I compile and run this program, the following is the output

Thread name is: t
Thread name is: t1

t2 does not execute the provided run() method and this could be because the run method is part of the Runnable class (or is it not?). I also extended the Thread class here.. now my question is.. Does run() method overridde the Thread class's run method? If yes, why does not t2 print the output and if not how can I make t2 run any code?

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

Originally posted by Dee Irun:
Thread t2 = new Thread(g, "thread");
t2.setName("t2");



Hi,

t2 will execute MyThread's run() only if you provide MyThread instance as the target to t2 while creating it, as in
Thread t2 = new Thread(g, new MyThread(), "thread");

If you do not provide a target that implements run(), then Thread's run() will be executed.

-------------------------------------------
public class Thread
extends Object
implements Runnable

void 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.

---------------------------------------------

- Soumya.
[ April 26, 2005: Message edited by: soumya ravindranath ]
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oui sowmya!
exactement.I agree with u.
jane
 
Dee Irun
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you do not provide a target that implements run(), then Thread's run() will be executed.

-------------------------------------------
public class Thread
extends Object
implements Runnable

void 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.



In my code I have both extended the Thread object and implemented Runnable interface. Does it mean that I have overridden the run() method of Thread class and implemented run() method for Runnable interface too??
 
soumya ravindranath
Ranch Hand
Posts: 300
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Dee Irun:


In my code I have both extended the Thread object and implemented Runnable interface. Does it mean that I have overridden the run() method of Thread class and implemented run() method for Runnable interface too??



Well, first of all, if you extend from Thread class, you need not additionally implement Runnable interface (why are you doing it, by the way ?). Yes, in your case, when you write a body for run() method, you are implementing the Runnable interface as required as well as overriding the parent Thread's run() method.

Any experts out there with a different opinion ?
 
That which doesn't kill us makes us stronger. I think a piece of pie wouldn't kill me. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic