Friday 20 July 2012

extends vs runnable


Extend vs Runnable
     class     Extends   Thread
class Myclass Extends Thread
class Implements Runnable
class Myclass implements Runnable
Thread is class
Thread is a class that’s why
We have to use extends key word
Runnable is an interface
Runnable is an interface so we hv
use implements key word
Does not support the mltiinheritence
If you want to extend the Thread class then it will make your class unable to extend other classes as java is having single inheritance feature
class Mine extends Thread,HttpServlet
  Not possible in java
support the mltiinheritence
If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems
class Mine implements Runnable
,Serializable extends HttpServlet

 Does not override the run() method to start() the thread. No compile and run time exceptions.
class Mine extends Thread{
public static void main(){
new Mine().start();
}
}
 should override (implements) the run() method otherwise it gives compile time error
class Mine implements Runnable{
public void run(){
System.out.println(“run”);
}
public static void main(){                                           Thread t = new Thread(new Mine());                                           t.start();
}

  each of your thread creates unique object and associate with it. (plz go through the below example to know the difference) it shares the same object to multiple threads
(plz go through the below example to know the difference)

class ImplementsRunnable implements Runnable {
private int counter = 0;
public void run() {
counter++;
System.out.println(“ImplementsRunnable : Counter : ” + counter);
}
}
class ExtendsThread extends Thread {
private int counter = 0;
public void run() {
counter++;
System.out.println(“ExtendsThread : Counter : ” + counter);
}
}
public class ThreadVsRunnable {
public static void main(String args[]) throws Exception {
//Multiple threads share the same object.
ImplementsRunnable rc = new ImplementsRunnable();
Thread t1 = new Thread(rc);
t1.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread
Thread t2 = new Thread(rc);
t2.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread
Thread t3 = new Thread(rc);
t3.start();
//Creating new instance for every thread access.
ExtendsThread tc1 = new ExtendsThread();
tc1.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread
ExtendsThread tc2 = new ExtendsThread();
tc2.start();
Thread.sleep(1000); // Waiting for 1 second before starting next thread
ExtendsThread tc3 = new ExtendsThread();
tc3.start();
}
}
Output:
ImplementsRunnable : Counter : 1
ImplementsRunnable : Counter : 2
ImplementsRunnable : Counter : 3
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1
ExtendsThread : Counter : 1
If you implement runnable interface, you can gain better object-oriented design and consistency and also avoid the single inheritance problems
each of your thread creates unique object and associate with it.
In the Runnable interface approach, only one instance of a class is being created and it has been shared by different threads. So the value of counter is incremented for each and every thread access.
Whereas, Thread class approach, you must have to create separate instance for every thread access. Hence different memory is allocated for every class instances and each has separate counter, the value remains same, which means no increment will happen because none of the object reference is same.
When to use Runnable?
Use Runnable interface when you want to access the same resource from the group of threads. Avoid using Thread class here, because multiple objects creation consumes more memory and it becomes a big performance overhead.
Apart from this, object oriented designs have some guidelines for better coding.

Coding to an interface rather than to implementation. This makes your software/application easier to extend. In other words, your code will work with all the interface’s subclasses, even ones that have not been created yet.
Interface inheritance (implements) is preferable – This makes your code is loosely coupling between classes/objects.(Note : Thread class internally implements the Runnable interface)
Example: coding to an interface.
Map subject = new HashMap();
Assigning HashMap object to interface Map,  suppose in future if you want to change HashMap to Hashtable or LinkedHashMap you can simple change in the declaration part is enough rather than to all the usage places. This point has been elaborately explained here.
Which one is best to use?
Ans : Very simple, based on your application requirements you will use this appropriately. But I would suggest, try to use interface inheritance i.e., implements Runnable.

No comments:

Post a Comment