[Java Threads] Should we extend Thread Class or implement Runnable interface


These are very common questions that a beginner Java developer may ask while learning Multi-threading in Java programming.

What are the ways in which one can create a Java Thread ?

Answer : There are two ways. 1 : Using Thread class, 2 : Using Runnable Interface.

What is the difference between Thread and Runnable ?

Answer : Thread is a class from java.lang.Thread package , where as Runnable is an interface from java.lang.Runtime package.

Should use Thread Class or Runnable interface and Why?

Answer : It depends, but as much as possible we must make use of Runnable interface, We must try to extend a class only when we want to override some behavior. When we extend a class, the two classes are very tightly coupled to each other.

Example: Thread Class: hreadClassExample.java
package com.code2care.examples;

public class ThreadClassExample extends Thread {

	Thread thread;

	ThreadClassExample() {

		thread = new Thread(this);
		System.out.println(thread + " : Started!");
		thread.start();
	}

	@Override
	public void run() {

		for (int i = 1; i <= 10; i++) {
			System.out.println("Thread : " + thread.getName() + " " + i);
			
		}
    }

}
File: ThreadClient.java
package com.code2care.examples;

public class ThreadClient {

	public static void main(String[] args) {

		new ThreadClassExample();
	
	}
}
Example: Runnable Interface: RunnableExample.java
package com.code2care.examples;

public class RunnableExample implements Runnable {

	Thread thread;

	RunnableExample() {

		thread = new Thread(this);
		thread.start();

	}

	@Override
	public void run() {

		for (int i = 1; i <= 10; i++) {
			System.out.println("Thread : " + thread.getName() + " " + i);

		}
	}
}
ThreadClient.java
package com.code2care.examples;

public class ThreadClient {

	public static void main(String[] args) {

		new ThreadClassExample();
	}
}
Advertisements
Try Out Code2care Dev Tools:

Advertisements



#Android #Java #Linux #Microsoft #Google #Python #MacOS #Notepad++ #Microsoft Teams #CSS #PHP #SharePoint #Html #Linux #C-Program #Bootstrap #jQuery #Sublime #Android-Studio #Facebook #Eclipse #WhatsApp #News #MySQL #Json #HowTos #JavaScript #FTP #S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency