File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread Doubt

 
Divya Gehlot
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O/p of below porogram is
MyThread: start()
MyRunnable: run()
Can any one explain why o/p is not
MyThread: start()
MyThread: run()
MyRunnable: start()
MyRunnable: run()



 
Praveen Seluka
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi divya

I will try my best to answer.

mythread.start()
The above statement is not starting the new thread.It just calls the overridden start() method in MyThread class.

Thats why you got the output : MyThread: start()

thread.start();
This statement starts the newly created thread.This will access start() method declared in Thread class which in turn calls run() method declared in MyRunnable class which results in the output MyRunnable: run()

mythread.start() calls the start() of MyThread alone
thread.start() calls the start() of Thread class which calls run() of MyRunnable.

Thanks
Praveen SP
 
dolly shah
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got the output. But I have another question. Why MyRunnable class is able to implement start() method. It is not a Runnable interface method.
can you explain?
 
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
Originally posted by dolly shah:
I got the output. But I have another question. Why MyRunnable class is able to implement start() method. It is not a Runnable interface method.
can you explain?


For the same reason the MyRunnable class is able to implement a method named join(), alive(), hello(), goodbye(), mymethod1(), mymethod2(), etc. Just because you implement an interface, doesn't mean that you are not allowed to have methods that is not part of that interface.

Henry
 
dolly shah
Ranch Hand
Posts: 383
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even Object class doesn't have start() method. From where MyRunnable can implement this method?
 
Michael Sanchez
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class may define new methods - there is no requirement that methods defined in a class be first declared in an interface.

However, *if* a class *does* implement an interface, then it must implement all of the methods declared in that interface (or be declared abstract itself).
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic