File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ThreadGroup question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ThreadGroup question" Watch "ThreadGroup question" New topic
Author

ThreadGroup question

Dee Irun
Greenhorn

Joined: Nov 13, 2004
Posts: 19
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!
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
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 ]
Jane W pemberley
Greenhorn

Joined: Apr 26, 2005
Posts: 12
Oui sowmya!
exactement.I agree with u.
jane
Dee Irun
Greenhorn

Joined: Nov 13, 2004
Posts: 19
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

Joined: Jan 26, 2001
Posts: 300
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 ?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ThreadGroup question