# threads

> A thread is a lightweight, independent, and concurrent unit of execution within a program

Threads allow multiple tasks to be performed concurrently, and each stack has its own call stack & program counter

## Ways to create a thread

1. extends `Thread`

```java
public class ThreadExtend extends Thread {

    public void run(){
        for (int i = 0; i<3 ; i++) {
            System.out.println("This is the class that extends Thread");

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
```

2. implements `Runnable`

```java
public class ThreadImplementRunnable implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i<3 ; i++) {
            System.out.println("This is the class that implements Runnable interface!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
```

Both have similar implementations , but to use them ...

```java
public class Main {
    public static void main(String[] args) throws InterruptedException {

        // Used when you want to create a specialized type of thread
        ThreadExtend threadExtend = new ThreadExtend();

        threadExtend.start();

        //  Use `implements Runnable` you are defining a task ThreadImplementRunnable() that can be executed by a thread
        ThreadImplementRunnable threadImplementRunnable = new ThreadImplementRunnable();
        Thread thread = new Thread(threadImplementRunnable);

        thread.start();
    }
}
```

### When to use which implementation

`extends Thread`

> Creates the functionality directly in a thread

Use this when you want to create a new type of thread with customized behavior

`implements Runnable`

> Creates a functionality which can be used in the constructor of a thread

Use this when you want to define a task that can be executed by multiple threads without creating new thread types.

## Thread Lifecycle

![Thread Lifecycle](/files/3rdra6Q88tk00jw6nBG7)

## Daemon Threads

> Daemon threads are low-priority threads that run in the background, providing services to non-daemon (user) threads.

The key distinction is that the JVM doesn't wait for daemon threads to finish before it exits. If all non-daemon (user) threads have completed, the JVM will exit, even if daemon threads are still running.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://aloy.gitbook.io/notes/languages-frameworks/programming-languages/java/concepts/threads.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
