---
title: "Threads in Java"  
description: "A thread is a lightweight independent process. Threading allows multiple processes to exist in one. Most of the modern operating system supports threa"  
author: "Prateek sharma"  
published: 2018-01-29  
updated: 2018-01-29  
canonical: https://www.mindstick.com/blog/11678/threads-in-java  
category: "java"  
tags: ["android", "java"]  
reading_time: 2 minutes  

---

# Threads in Java

A thread is a lightweight [independent](https://answers.mindstick.com/qa/98599/what-is-the-longest-river-in-the-commonwealth-of-independent-states) process. [Threading](https://www.mindstick.com/blog/187/threading-in-c-sharp) allows [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) processes to exist in one. Most of the modern [operating system](https://www.mindstick.com/articles/229069/operating-system-development) supports threads. Threads are independent, have concurrent execution, local [variables](https://www.mindstick.com/articles/715/php-variables), and its own stack.

There can exist multiple threads within a process which means that these threads will have same memory address space, this will allow a thread to share information with each other. It must be taken care that threads do not interfere with each other.

## Why use threads?

Reasons for using the [thread as](https://www.mindstick.com/interview/2494/can-we-make-the-user-thread-as-daemon-thread-if-thread-is-started) they help to:

1. Make UI more responsive.
2. Gives the advantage of a multiprocessor system.
3. Makes the modeling easy
4. Perform [asynchronous](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) or [background processing](https://answers.mindstick.com/qa/99879/how-does-android-handle-background-processing-and-multitasking).

A simple example of a thread in Java by extending thread class.

```
class Multi extends Thread{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi t1=new Multi();
t1.start();
 }
}
```

**[public void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) run()**: is used to perform some action for a thread.

Threads can also be implemented using Runnable interface. Following is the example

```
class Multi3 implements Runnable{
public void run(){
System.out.println("thread is running...");
}
public static void main(String args[]){
Multi3 m1=new Multi3();
Thread t1 =new Thread(m1);
t1.start();
}
}
```

\

---

Original Source: https://www.mindstick.com/blog/11678/threads-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
