| Author |
Extending Thread Chaos
|
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
Consider the following code
1 : In the above code i am making the Thread class Object with the Runnable instance but still getting unexpected output.
2: I upcasted it, still getting the same output.
My expected Output is : "I got Threads " but it is not
Thanks !!!
|
SCJP 6.0 96%
(Connecting the Dots ....)
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
|
Look at this discussion (there were many other discussions like that too in the past)...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
It is not clear from the linked discussion.....That why it is all happening.....
Moreover i upcasted it.....even then it is not working.....
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
This is how Thread class is implemented , then if i passing "runnable" to Thread class.....Its run method should be called as Line 1 in the above code says.....
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
Sahil Kapoor wrote:
This is how Thread class is implemented , then if i passing "runnable" to Thread class.....Its run method should be called as Line 1 in the above code says.....
Hint: When you override a method, which version of the method will get called?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
|
You are right Henry , But i am making Thread object by passing Runnable object, and according to book, if we pass runnable, runnable's run method would be invoked.
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
Ooooppppssss Got it ..........FInally !!!
Thanks Henry !!!
Cheers!!!
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
@ For others.... who got confused.....IMO implementation (partial) is as follows
|
 |
Suresh Babu Venkat
Ranch Hand
Joined: Sep 15, 2009
Posts: 30
|
|
Sahil Kapoor wrote:
That line of the program should have given a compile time error as we cannot instantiate an interface.
|
SCJP 6.0 (88%) | SCWCD 5.0 (89%)
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
Suresh Babu Venkat wrote:
Sahil Kapoor wrote:
That line of the program should have given a compile time error as we cannot instantiate an interface.
If you have covered Anonymous classes then i am using that.
If you have not done then, i am not creating an instance of runnable , in fact i am creating an instance of class that implements runnable and the implementig class body is defined after new Runnable {//here}.
Thanks!!!
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16684
|
|
Sahil Kapoor wrote:
I know that this is only supposed to be "explanation" code, but I have to point out that this part is not true.
The start() method, or the thread that called the start() method, does not call the run() method. The start() method creates a new thread, and part of the process, after start up, of the newly created thread, is to call the run() method.
Henry
|
 |
Suresh Babu Venkat
Ranch Hand
Joined: Sep 15, 2009
Posts: 30
|
|
Sahil Kapoor wrote:If you have covered Anonymous classes then i am using that.
If you have not done then, i am not creating an instance of runnable , in fact i am creating an instance of class that implements runnable and the implementig class body is defined after new Runnable {//here}.
ooopssss... I overlooked it.... thanks for the explanation.
|
 |
Sahil Kapoor
Ranch Hand
Joined: Sep 12, 2009
Posts: 316
|
|
@ Henry , yess your right , IS the following pseudocode fine now :-
|
 |
 |
|
|
subject: Extending Thread Chaos
|
|
|