Wednesday 26 April 2017

4. Create a Thread using Runnable interface

1. How to create a thread using Runnable interface
  • Way : 1
    1. Create a custom class with implements Runnable interface.
    2. Override run() from Runnable interface.(Runnable interface contains only one method named run())
    3. Write business logic in run().
    4. Create an object for custom class(implements Runnable interface).
    5. Creates an object for Thread class, and pass above created object(implements Runnable interface).
    6. Call start() through the thread object.

public class ThreadDemo implements Runnable{

@Override
public void run() {
for(int i=0; i<10; i++){
System.out.println("i am in run()");
}
System.out.println("end of run()");
}
public static void main(String[] args){
ThreadDemo t1 = new ThreadDemo();
Thread thread = new Thread(t1);
thread.start();
System.out.println("end of main()");
}

}


  • Way : 2
    1. Create a custom class.(No implementations)
    2. Creates an object for Thread class, and pass the parameter as anonymous class which is implementation of Runnable interface and overrides run()
    3. Call start() through the thread object.
Thread t = new Thread(new Runnable(){
@Override
public void run() {
}
});


public class ThreadDemo {

public static void main(String[] args) {

System.out.println("start of main() by "+Thread.currentThread().getName());

Thread thread = new Thread(new Runnable() {
@Override
public void run() {
for(int i=0; i<10; i++){
System.out.println("i am in run() by "+Thread.currentThread().getName());
}
System.out.println("end of run() by "+Thread.currentThread().getName());
}
});
thread.start();
System.out.println("end of main() by "+Thread.currentThread().getName());
}

}


  • Way : 3
    1. Create a custom class.(No implementations)
    2. Creates an object for Thread class, and pass the parameter as anonymous class which is implementation of Runnable interface by using lambda expressions and overrides run()
    3. Call start() through the thread object.
Thread t = new Thread(() -> {System.out.println("My Runnable");});
t.start();


public class ThreadDemo {

public static void main(String[] args) {

System.out.println("start of main() by "+Thread.currentThread().getName());
Thread thread = new Thread(() -> {
for(int i=0; i<10; i++){
System.out.println("i am in run() by "+Thread.currentThread().getName());
}
System.out.println("end of run() by "+Thread.currentThread().getName());
}
);
thread.start();
System.out.println("end of main() by "+Thread.currentThread().getName());
}

}


2. Difference between Threads creation by using Thread class and Runnable interface  
·         In both the ways, we need to override run() in sub class with logic that should be executed in user defined thread concurrently while calling start() on Thread class object to create thread of execution in java stack area.
·         Example for create a thread by extending Thread class.
public class MyThread extends Thread {
  @Override
  public void run() {
        System.out.println("I am in run()");
  }
  public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
  }
}
·         Example for create a thread by implementing Runnable interface.
public class MyThread implements Runnable {
  @Override
  public void run() {
        System.out.println("I am in run()");
  }
  public static void main(String[] args) {
        Thread thread = new Thread(new MyThread());
        thread.start();
  }
}
1) Multiple Inheritance Limitation
As you know, Java doesn’t support multiple inheritance. A class in java can extend only one class. If you extend Thread class, then your class will not be able to extend any other class. This will limit your class to thread behavior. If you implement Runnable interface, then you will have an option for your class to extend any other class and inherit behaviors from other class also.
2) Overhead Of Additional Methods
If you extend Thread class, all methods of Thread class will be inheriting to your class which you may not need. This will cause additional overhead. You can remove this overhead by implementing Runnable interface.
3) Logical Separation Of Task From The Runner
If you implement Runnable interface, it will separate actual task from the runner. Runnable interface represents only the task and you can pass this task to any type of runner, either a thread or any executors.
4) Best Object Oriented Design Practice
In object oriented programming, extending a class means modifying or improving the existing class. If you are not improving the class, then it is not a good practice to extend it. So, implementing Runnable will be the best object oriented design practice.
5) Loosely Coupled Vs Tightly coupled
“Implements Runnable” makes your code loosely coupled. Because it separates the task from the runner. “Extends Thread” will make your code tightly coupled. Because, single class will act as both task container as well as runner.
6) Reusability
Implementing Runnable improves the reusability of your code. Because, Runnable contains only the task and you can use it wherever and whenever you want.

Implements Runnable
Extends Thread
You can extend any other class.
You can’t extend any other class.
No overhead of additional methods .
Overhead of additional methods from Thread class.
Separates the task from the runner.
Doesn’t separate the task from the runner.
Best object oriented programming practice.
Not a good object oriented programming practice.
Loosely coupled.
Tightly coupled.
Improves the reusability of the code.
Doesn’t improve the reusability of the code.
More generalized task.
Thread specific task.
Maintenance  of the code will be easy.
Maintenance of the code will be time consuming.



No comments:

Post a Comment

3. Java Program to create Binary Tree