Thursday, 6 September 2018

Implement Runnable vs extends Thread

You can create a thread using either of below ways.
         a. Extending a Thread class
         b. Implementing a Runnable interface.
        
Extending a Thread class
Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

public class Application {
 private static class PrinterThread extends Thread {
  private int counter = 1;

  public void run() {
   while (true) {
    System.out.println("PrinterThread : " + counter++);
    try {
     TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }

 public static void main(String args[]) {
  PrinterThread thread = new PrinterThread();
  thread.start();
 }
}

Output
PrinterThread : 1
PrinterThread : 2
PrinterThread : 3
PrinterThread : 4
PrinterThread : 5
PrinterThread : 6
PrinterThread : 7
...........
...........
...........

Implementing Runnable interface

Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;

public class Application {
 private static class PrinterTask implements Runnable {
  private int counter = 1;

  public void run() {
   while (true) {
    System.out.println("PrinterThread : " + counter++);
    try {
     TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }

 public static void main(String args[]) {
  Thread thread = new Thread(new PrinterTask());
  thread.start();
 }
}

Output
PrinterThread : 1
PrinterThread : 2
PrinterThread : 3
PrinterThread : 4
PrinterThread : 5
PrinterThread : 6
PrinterThread : 7
.............
.............
.............

Runnable Vs Thread class
a. Since Java do not support multiple inheritance, once you create a thread by extending Thread class, you can't extend other class. But if you create a thread by implementing Runnable interface, you have an option to extend other class.

b. Many threads can share same runnable instance.


Application.java
package com.sample.app;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class Application {
 private static class PrinterTask implements Runnable {
  private AtomicInteger counter = new AtomicInteger();

  public void run() {
   while (true) {
    System.out.println(Thread.currentThread().getName() + " => " + counter.incrementAndGet());
    try {
     TimeUnit.SECONDS.sleep(2);
    } catch (InterruptedException e) {
     e.printStackTrace();
    }
   }
  }
 }

 public static void main(String args[]) {
  PrinterTask task = new PrinterTask();

  Thread thread1 = new Thread(task);
  thread1.setName("Thread1");

  Thread thread2 = new Thread(task);
  thread2.setName("Thread2");

  Thread thread3 = new Thread(task);
  thread3.setName("Thread3");

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

Sample Output
Thread3 => 2
Thread2 => 3
Thread1 => 1
Thread2 => 6
Thread1 => 4
Thread3 => 5
Thread3 => 7
Thread1 => 8
Thread2 => 9
Thread3 => 10
Thread1 => 11
Thread2 => 12
.......
.......

You may like

No comments:

Post a Comment