Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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
Interview Questions
Programming Questions
Miscellaneous
Threads: print characters of a string simultaneously
Why Java compound assignment operators do not require casting?
Can I call one constructor from another in java?
Is Arrays.asList immutable
How to create immutable list in Java?
Open-closed principle design problem


This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Implement Runnable vs extends Thread

×

Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription

×