
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Difference between Thread and Runnable in Java
There are two ways to create a new thread of execution. One is to declare a class to be a subclass of the Thread class. This subclass should override the run method of the Thread class. An instance of the subclass can then be allocated and started.
The other way to create a thread is to declare a class that implements the Runnable interface. That class then implements the run method. An instance of the class can then be allocated, passed as an argument when creating Thread, and started.
Every thread has a name for identification purposes. More than one thread may have the same name. If a name is not specified when a thread is created, a new name is generated for it.
Sr. No. | Key | Thread | Runnable |
---|---|---|---|
1 | Basic | Thread is a class. It is used to create a thread | Runnable is a functional interface which is used to create a thread |
2 | Methods | It has multiple methods including start() and run() | It has only abstract method run() |
3 | Each thread creates a unique object and gets associated with it | Multiple threads share the same objects. | |
4 | Memory | More memory required | Less memory required |
5 | Limitation | Multiple Inheritance is not allowed in java hence after a class extends Thread class, it can not extend any other class | If a class is implementing the runnable interface then your class can extend another class. |
Example of Runnable
class RunnableExample implements Runnable{ public void run(){ System.out.println("Thread is running for Runnable Implementation"); } public static void main(String args[]){ RunnableExample runnable=new RunnableExample(); Thread t1 =new Thread(runnable); t1.start(); } }
Example of Thread
class ThreadExample extends Thread{ public void run(){ System.out.println("Thread is running"); } public static void main(String args[]){ ThreadExample t1=new ThreadExample (); t1.start(); } }
- Related Articles
- Difference Between Thread Class and Runnable Interface in Java
- Difference between Runnable and Callable interface in java
- Implement Runnable vs Extend Thread in Java
- How to create a thread without implementing the Runnable interface in Java?
- Difference between Process and Thread
- Difference between Fixed thread pool and cached thread pool.
- Difference between Goroutine and Thread in Golang.
- Difference between green and native thread
- What is Runnable interface in Java?
- Difference between scheduledThread pool and Single Thread Executor.
- User Thread vs Daemon Thread in Java?
- Lifecycle and states of thread in java
- Daemon thread in Java
- Thread Pools in Java
- How to implement the Runnable interface using lambda expression in Java?

Advertisements