In this tutorial, we will learn ways to create a Thread in Java. A Thread is a lightweight process and Java has a feature called multithreading which means we can run multiple threads simultaneously. There are two ways to create the thread in Java first one is by extending Thread class and the second one is by implementing Runnable interface.

In Java, we can inherit Thread class in our class to create threads. Thread class contain constructors and methods which makes it so easy to create and maintain a thread. Check out these few steps to create a thread using Thread class.

• Override the run() method in your class.

Check out the following example. To understand it more wisely.

## Example Program:

``````//Inherting Thread class in Example class
//Overiding run method
public void run(){
for(int i=0;i<5;i++){
System.out.println(i);
}
}

public static void main(String args[]){
Example obj=new Example();
obj.start(); //using start() method to start thread
}
}``````

## Output:

``````0
1
2
3
4``````

## Creating a thread using the Runnable interface:

We know Multiple Inheritance is not allowed in Java and we know to perform multi-threading we have to inherit Thread class in our class. But in some scenarios, if a class already inheriting another class we cannot inherit Thread class. So in this kind of situation, we can implement Runnable interface to create a thread in class.

Runnable interface contains only one method which is run() method.

## Example Program:

``````//Implementing Runnable interface in Example class
public class Example implements Runnable{
//Overiding run method
public void run(){
for(int i=0;i<5;i++){
System.out.println(i);
}
}

public static void main(String args[]){
Example obj=new Example();
}
}``````

## Output:

``````0
1
2
3
4``````

It is important to note we know Runnable interface contains an only a single method which is run(). But we need the start() method to start a thread. So to get this start method we create an object of Thread class(check 12th line in the above example code) and we pass the object of our class in the constructor of Thread class. After that, we called the start method using thread class object.

Here is another example program. In which you can see how we can directly define and override run() method in the constructor of the Thread class.

## Example Program:

``````public class Example {
public static void main(String args[]){
public void run(){
for(int i=0;i<5;i++){
System.out.println(i);
}
}
});
}
}``````

## Output:

``````0
1
2
3
4``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.79277194,"math_prob":0.6548984,"size":2874,"snap":"2021-43-2021-49","text_gpt3_token_len":622,"char_repetition_ratio":0.17526132,"word_repetition_ratio":0.15517241,"special_character_ratio":0.2362561,"punctuation_ratio":0.11371841,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95995927,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-23T07:39:14Z\",\"WARC-Record-ID\":\"<urn:uuid:32bf4c87-0a91-4c66-b52e-d9e0d9c306d9>\",\"Content-Length\":\"33553\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7d29cb88-db68-4e65-85c6-a77ba2209035>\",\"WARC-Concurrent-To\":\"<urn:uuid:436a2ca9-0c89-4713-93b4-a5d774e6e155>\",\"WARC-IP-Address\":\"151.106.116.198\",\"WARC-Target-URI\":\"https://owlbuddy.com/thread-in-java\",\"WARC-Payload-Digest\":\"sha1:7ZFN5TX2U4F44EFAIN3L6FIJELBIDS3O\",\"WARC-Block-Digest\":\"sha1:CYFT4V5J2ZBW4AEVZG7JFFNU2CLCPCYM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585653.49_warc_CC-MAIN-20211023064718-20211023094718-00225.warc.gz\"}"}