• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread Doubt

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()



 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even Object class doesn't have start() method. From where MyRunnable can implement this method?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic