# Java Thread

\ <br>

### Concurrency

<br>

* Concurrency
  * Logical
  * Single Core / Multi Core
* Parallel
  * Physical
  * Multi Core

<br>

*Since Java runs on the **JVM (Java Virtual Machine)**, you don't need to worry about the physical aspects!*

\ <br>

### What is a Java Thread?

<br>

* `Process`

  : A program that operates independently (Eclipse, Messenger, etc)
* `Thread`

  : A small unit of execution that constitutes a Process (Messenger = chat + file transfer)
* `Multi-process`

  : Running multiple processes simultaneously
* `Multi-thread`

  : Multiple threads operating simultaneously within a single process

\ <br>

### Creating a Java Thread

<br>

#### Method 1) Implementing the Runnable interface

* Runnable interface

  ex)

  ```java
  package virus;

  public class CoronaRunnable implements Runnable{

  	int num;
  	public CoronaRunnable() {}
  	public CoronaRunnable(int num) {
  		this.num = num;
  	}
  	@Override
  	public void run() {
  		for (int i =0 ; i< 10000 ; i++) {
  			int j = i*100;
  		}
  		System.out.println(num);
  	}
  }
  ```

  <br>
* Test

  ```java
  package app;

  import virus.CoronaRunnable;

  public class CoronaThreadTest {
  	public static void main(String[] args) {
  		for (int i = 0 ; i <1000 ; i++) {
  			CoronaRunnable cr = new CoronaRunnable(i);
  			Thread t = new Thread(cr);
  			// Start the Thread
  			t.start();
  		}
  	}
  }
  ```

<br>

#### Method 2) Extending the Thread class

* Thread class

  ex)

  ```java
  ```

package virus;

public class CoronaThread extends Thread{ int num;

```
public CoronaThread() {}
public CoronaThread(int num) {
	this.num = num;
}

@Override
public void run() {
	for (int i = 0 ; i < 10000 ; i++) {
		int j = i*100;
	}
	System.out.println(num);
}
```

}

````

<br>

- Test

```java
import virus.CoronaRunnable;
import virus.CoronaThread;

public class CoronaThreadTest {
	public static void main(String[] args) {
		for (int i = 0 ; i <1000 ; i++) {
			CoronaThread ct = new CoronaThread(i);
			ct.start();
		}
	}
}
````

\ <br>

### Memory Structure When Running a Thread

\ <br>


---

# 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://chloe-codes1.gitbook.io/til/java/java-advanced/01_thread.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.
