
- 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
How to create a thread without implementing the Runnable interface in Java?
A Thread can be called a lightweight process. Java supports multithreading, so it allows our application to perform two or more task concurrently. All Java programs have at least one thread, known as the main thread, which is created by the Java Virtual Machine (JVM) at the program start when the main() method is invoked with the main thread. There are two ways to create a thread in Java, by extending a Thread class or else by implementing the Runnable interface.
We can also create a thread without implementing the Runnable interface in the below program
Example
public class CreateThreadWithoutImplementRunnable { // without implements Runnable public static void main(String[] args) { new Thread(new Runnable() { public void run() { for (int i=0; i <= 5; i++) { System.out.println("run() method of Runnable interface: "+ i); } } }).start(); for (int j=0; j <= 5; j++) { System.out.println("main() method: "+ j); } } }
Output
main() method: 0 run() method of Runnable interface: 0 main() method: 1 run() method of Runnable interface: 1 main() method: 2 run() method of Runnable interface: 2 main() method: 3 run() method of Runnable interface: 3 main() method: 4 run() method of Runnable interface: 4 main() method: 5 run() method of Runnable interface: 5
- Related Articles
- Difference Between Thread Class and Runnable Interface in Java
- What is Runnable interface in Java?
- How to implement the Runnable interface using lambda expression in Java?
- Implement Runnable vs Extend Thread in Java
- Difference between Thread and Runnable in Java
- Difference between Runnable and Callable interface in java
- How to create a thread in Java
- How to create a thread in JShell in Java 9?
- How to create a thread using method reference in Java?\n
- How to create a thread by using anonymous class in Java?
- How to create a thread using lambda expressions in Java?\n
- How to create a thread in android?
- How to create a thread in C#?
- Can we override only one method while implementing Java interface?
- What method is used to create a daemon thread in Java?

Advertisements