Implementing Threads in java by implementing Runnable interface and extending Thread class


Contents of page :
  • Threads can be created in two ways
  • Program to create thread implementing java.lang.Runnable >
  • Program to create thread extending java.lang.Thread >
  • We should implement Runnable interface or extend Thread class ?

Threads can be created in two ways i.e. by implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.


Thread has its own variables and methods, it lives and dies on the heap. But a thread of execution is an individual process that has its own call stack. Thread are lightweight process in java.

  1. Thread creation by  implementing java.lang.Runnable interface.
We will create object of class which implements Runnable interface :

MyRunnable runnable=new MyRunnable();
Thread thread=new Thread(runnable);


   2) And then create Thread object by calling constructor and passing reference of Runnable interface i.e.  runnable object :

Thread thread=new Thread(runnable);



Program to create thread implementing java.lang.Runnable >

Create Runnable object, pass it in thread constructor. But our thread is not going to start until we call thread.start(), calling start() method internally calls run() method.
class MyRunnable implements Runnable{
   public void run(){   //overrides Runnable's run() method
          System.out.println("in run() method");
   }
}

/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class MyClass {
   public static void main(String args[]){
          MyRunnable runnable=new MyRunnable();
          Thread thread=new Thread(runnable);
          thread.start();
   }
}
/*OUTPUT
in run() method
*/



Program to create thread extending java.lang.Thread >

Thread creation by extending java.lang.Thread class.
We will create object of class which extends Thread class :
MyThread obj=new MyThread();

But our thread is not going to start until we call obj.start()
calling start() method calls run() method.
class MyThread extends Thread{
   public void run(){   //overrides Thread's run() method
          System.out.println("in run() method");
          System.out.println("currentThreadName= "+ Thread.currentThread().getName());
   }
}
public class MyClass {
   public static void main(String args[]){
            System.out.println("currentThreadName= "+ Thread.currentThread().getName());
     MyThread obj=new MyThread();  
          obj.start();
   }
}
/*OUTPUT
currentThreadName= main
in run() method
currentThreadName= Thread-1
*/
If we note output of program, we will find that program started with main thread, and then calling obj.start() called run() method with Thread-1



We should implement Runnable interface or extend Thread class ?
Well the answer is you must extend Thread only when you are looking to modify run() and other methods as well. If you are simply looking to modify only the run() method implementing Runnable is the best option (Runnable interface has only one abstract method i.e. run() ).




RELATED LINKS>

Thread basics >

What is thread in java

Thread states/ Thread life cycle in java

Difference between Process and Thread in java

Implementing Threads in java by implementing Runnable interface and extending Thread class

Threads implement their own stack - demonstration using program and diagram

Differences between implementing Runnable interface and extending Thread class

Thread behaviour is unpredictable

When threads are not lightweight process in java, in fact they become heavy weight process


Consumer Producer problem solution using different techniques >
Solve Consumer Producer problem by using wait() and notify() methods in multithreading

solve Consumer Producer problem by using wait() and notify() methods, where consumer can consume only when production is over


No comments:

Post a Comment