• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

thread

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is difference between implements runnable and extends thread
give me some examples.
 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The reason for this is Java's tricky way of implementing multiple inheritance. Many OO Languages allow multiple inheritance, which means you can create a Child which have two parents and will inherit the properties of both the parents. It is however confusing and Java does not allow that.
So to create a class which uses threads you have to be a child of (in java terminology we have to "implement") Runnable. Thread class implements Runnable and hence it is a good idea to extend Thread to create your class. But in some cases if you creating a class "Dog", and a "Mammal" class is already created, it makes sense to create Dog as a child of Mammal. You may not create it as child of Thread even if you use threads in Dog. In that case you may have to implement Runnable.
The major diffence in the usage are:
If you are implementing Runnable and if your class is creating multiple Threads, all the threads acts on the same instance variables of your class. In other words you will have only once instance of your class and will have multiple instances of Thread class. All the threads run the same run method of your Instance and hence share the instance variables of your instance.
If you are extending Thread and use that class to create multiple Threads then you will have once instance of your Class for each thread you create:
public class MyClass extends Thread {
--
}
You use something like this to create multiple threads:
MyThread t1 = new MyThread();
MyThread t2 = new MyThread();
The first thread will act on variables in Object t1 whereas second one runs against Object t2. So both the Threads will act on different instance variables.
I could think of only this difference -- If anybody have more to add please feel free to do so.
------------------
Hari Gangadharan
Out of the turbulance a soft voice spoke to me ...
Son smile and be happy things could be worse...
I smiled and I was happy and things became worse!
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic