Creating Thread using Extend and Runnable in Java

In Java, You can create thread using 2 ways.

1. Using extend Thread
  •  MyThread t1 = new MyThread("Thread t1"); 
  • where MyThread is class which extends Java's Thread Class.
2. Using implements Runnable
  •    MyRunnable runnable = new Runnable();
  •    Thread t1 = new Thread(runnable,"Thread t1");   
  •     where MyRunnable is class which implements Runnable Interface.

How You can Start Thread?
When you create Object is Thread and You can start Thread by calling ThreadObj.start();
Thread will be started and it internally calls run Method and Logic written in run method will be executed by calling Thread.

Which way to create Thread is better ? Creating Thread using "Extend Approach" or using "Runnable Approach"?
Since Java doesn't support multiple Inheritance, it's preferable to use "implements Runnable" approach

What will happen if I call run() method directly?
 If You call run method directly then It will not execute separate thread but will be executed by same Thread.
Ex. Considering second example mention below.
MyRunnableImpl runnable = new MyRunnableImpl();
Thread t1 = new Thread(runnable,"Thread t1");
Thread t2 = new Thread(runnable,"Thread t2");
Thread t3 = new Thread(runnable,"Thread t3");

 t1.start();
 //t2.start(); --> will print "Running Thread - Thread t2"
 t2.run(); --> will print "Running Thread - main
"
 t3.start();


Creating Thread using Extend
package com.anuj.threading;

/**
 * MultiThread Example
 *
 * @author Anuj
 *
 */
class MultiThreadExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        MyThread1 thread1 = new MyThread1("Thread 1");
        MyThread1 thread2 = new MyThread1("Thread 2");
        MyThread1 thread3 = new MyThread1("Thread 3");

        thread1.start();
        thread2.start();
        thread3.start();
    }
}

class MyThread1 extends Thread {

    MyThread1() {
    }

    MyThread1(String name) {
        this.setName(name);
    }

    @Override
    public void run() {
        System.out.println("Running Thread - " + Thread.currentThread().getName());
    }

}

Output:
Running Thread - Thread 1
Running Thread - Thread 3
Running Thread - Thread 2

Creating Thread using implements Runnable
package com.anuj.threading;

/**
 * MultiThread Runnable Example
 *
 * @author Anuj
 *
 */
class MultiThreadRunnableExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        MyRunnableImpl runnable = new MyRunnableImpl();
        Thread t1 = new Thread(runnable,"Thread t1");
        Thread t2 = new Thread(runnable,"Thread t2");
        Thread t3 = new Thread(runnable,"Thread t3");

        t1.start();
        t2.start();
        t3.start();
    }

}

class MyRunnableImpl implements Runnable{

    @Override
    public void run() {
        System.out.println("Running Thread - " + Thread.currentThread().getName());
    }

}

Output:
Running Thread - Thread t1
Running Thread - Thread t3
Running Thread - Thread t2

No comments:

Post a Comment