aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes threads! 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 "threads!" Watch "threads!" New topic
Author

threads!

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
hi all,
the code given below, when run displays "Running YourThread" but i think it should display "Running MyThread" since we are passing an instance of Mythread in the constructor of YourThread. Please explain where am i wrong?
thanks.
ashok.
___________
class MyThread implements Runnable {
public void run() {
System.out.println("Running MyThread");
}
}
class YourThread extends Thread {
YourThread(Runnable r) {
super(r);
}
public void run() {
System.out.println("Running YourThread");
}
}
class Test {
public static void main(String args []) {
MyThread t1= new MyThread();
YourThread t2 = new YourThread(t1);
t2.start();
}
}
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
You get "Running Your thread" because you've extended the Thread class and overide it's run method. t2.start calls the overridden run method of the yourThread class.

rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
hi all,
i am still not able to understand why the output is :"Running YourThread" and not "Running MyThread".Isn't the run() method of Runnable object executed when the Runnable object is passed as argument in Thread constructor?
pls throw some light on it.
rajashree.
swati bannore
Ranch Hand

Joined: Oct 18, 2000
Posts: 201
I just commented the run() of YourThread class, and then the output was "Running myThread"

Is it beacuse, since run() is overridden In YourThread, It executes the method of it's reference type..
Thanx

Swati Kale
SCJP
SCWCD
Gagan Indus
Ranch Hand

Joined: Feb 28, 2001
Posts: 346
Ashok ,
good question ! nice
well.. the the reason to seemingly-strange output of above code lies in two factors ;
- The way Thread's start() n run() hav been implemented
- Polymorphism n inheritence
The Thread class start() method is native , all it does is it will instruct JVM to spawn a new-thread in which to run the run() method of this thread .
Now carefully look below at the way run method has been implemented ;

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

( target is the one which we pass as Runnable in Thread's constructor )
All i mean to say is that start() method just calls the run() method , it DO NOT checks whether a Runnable was passed or not , and if was passed then to call Runnable's run() method instead of Thread's run method , somehow by design-choice , this functionality has been kept in run() method of Thread, instead of start().

Now , when we extends Thread class , and override run() method , wht we get is a class with ;
-start() method inherited from parent Thread()
-run() method of its own
A call to start() method on object of such a class , obviously spawan a new Thread with overriden run() method
( explanation to this lies in the property of Polymorphism , inheritence n method lookup tht is )

To make things understandable ( n experimentable , u cant bother to change original Thread implementation! ) , i have wrriten following code ,
Most of the hierachy corresponds to original thread classes , but i hav kept them simple according to our specific problem

not surprisingly , the output of above code is :
Doing YourWork

In bare-bones ,all wht-is-happening is due to design-choice , if instead of keeping runnable-check functionality in run() method (doing the target==null check n then calling its run) , if it wuld hav been kept in start() method , then result will hav been altogether diff.
Like in my code if we change start() n doit() method in Work class as :

public void start() { if(work != null) work.doit(); else doit(); }
public void doit() { /* do nothing */ } ;

The the output changes to :
Doing MyWork
Read this code once or twice , n try to experiment it for urself , n i am sure , u ll get my point.
BTW , it is pity tht there are no marks in SCJp for asking questions ! otherwise i wuld hav whole heartily given Ashok full marks !
nice question , keep discussing these kinda things
have chillin preparation n Good Luck !

[This message has been edited by Gagan Indus (edited September 09, 2001).]


Gagan (/^_^\) SCJP2 SCWCD IBM486 <br />Die-hard JavaMonk -- little Java a day, keeps you going.<br /><a href="http://www.objectfirst.com/blog" target="_blank" rel="nofollow">My Blog</a>
Vanitha Sugumaran
Ranch Hand

Joined: Apr 11, 2001
Posts: 356
Good question Ashok and Great explanation Gagan.
Vanitha.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: threads!