Report.md created and completed.

This commit is contained in:
2026-06-11 02:18:41 +03:30
parent 9e5c715088
commit bcb381f84b
+95
View File
@@ -0,0 +1,95 @@
### 1. `start()` vs `run()`
* #### output:
```bash
Calling run()
Running in: main
Calling start()
Running in: Thread-2
```
When we call `t1.run()`, Java does not create a new thread. This method runs like a normal method in the thread it was called on. That's why `Thread.currentThread().getName()` prints the name of the current thread, main.
But when we call `t2.start()`, the JVM creates a new thread in the background. JVM creates a new thread called Thread-2 and executes the `run()` method inside this new thread. That's why the resulting thread is named Thread-2.
* #### the difference in behavior between calling `start()` and `run()`
The main difference between these two methods:
1. how they manage memory
2. create new trades
`run()` method:
* No new thread
* Execution: Completely normal and line-by-line, The main thread of the program enters the run method, executes the code, and exits. Until this method finishes, subsequent lines of code in the main method wait.
`start()`
* New thread
* Execution: Completely parallel and asynchronous. As soon as you call `start()`, the new thread starts in the background, and the main thread immediately moves on to the next line of its code without waiting for the new thread to finish.
---
### 2. Daemon Threads
* #### output:
```bash
Main thread ends.
Daemon thread running... (multiple times)
```
Why?
In Java we have two types of threads: User Threads (user or main threads) and Daemon Threads (backup threads).
The Java Virtual Machine (JVM) continues to run as long as at least one user thread is alive. Once all user threads have finished, the JVM stops the application, even if daemon threads are still running.
In this code, the main thread is a user thread. When `thread.start()` is executed, the daemon thread starts running, but immediately on the next line, the main thread prints "Main thread ends." and terminates. Since no other user threads are alive, the JVM does not allow the daemon thread to complete its 20-thread loop; it immediately closes the entire program and the job stops.
* #### If we remove the line `thread.setDaemon(true)`
If we delete this line, this thread will become a user Thread and the program will no longer close immediately, and the output will be like this:
```bash
Main thread ends.
Daemon thread running...
Daemon thread running...
... (This phrase is printed exactly 20 times)
Daemon thread running...
```
* #### Real-life use cases
Daemon threads are typically used for tasks that are useful as long as the main application is open, but are no longer needed once the main application is closed.
Some real-world examples:
1. Garbage Collector: This trick runs continuously in the background to clear unused memory.
2. Auto-Save: A daemon thread in the background that saves our writing every few minutes.
3. Spell Checker: As we type, tread draws a red line in the background under misspelled words.
---
### 3. A shorter way to create threads
* #### output:
```bash
Thread is running using a ...!
```
* #### The name of this `syntax () -> { ... }`
This structure is called a Lambda Expression in Java.
The open and close parentheses `()` represent the method inputs (which are empty since the run method takes no parameters in its input). The arrow symbol `->` means take these inputs and put them inside the code inside the curly braces `{ ... }` to be executed.
* #### The difference
In terms of final performance and CPU behavior, there is no difference; the operating system creates a new thread in both cases. But the differences:
1. Eliminate Boilerplate Code: In the old way, we had to create a new class, write `public void run()` inside it, and add a bunch of opening and closing braces. Lambda removes all of this formality and lets us get straight to the “boilerplate” (the code that needs to be executed).
2. Using Functional Interface: Java is very smart. Since the Thread class constructor takes an input of type Runnable interface and this interface has only one method called `run()`, Java automatically understands that the code inside the lambda is going to replace that one `run()` method.
3. Preserve inheritance: When we write a class that extends Thread, due to Java limitations, we cannot inherit that class from any other class. But the lambda method (which is based on Runnable) does not impose this limitation and leaves us free to design our classes.
4. Separating the task from the executor: In this method, we inject the task into the execution engine (Thread) as a body of code, which is much more standard than merging the two together.
---
Done :)