File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending Thread Chaos

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at this discussion (there were many other discussions like that too in the past)...
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ooooppppssss Got it ..........FInally !!!
Thanks Henry !!!
Cheers!!!

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ For others.... who got confused.....IMO implementation (partial) is as follows


 
Suresh Babu Venkat
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Kapoor wrote:





That line of the program should have given a compile time error as we cannot instantiate an interface.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Henry , yess your right , IS the following pseudocode fine now :-

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic