redmi2 + multithreading

This commit is contained in:
2026-06-03 00:17:08 +03:30
parent 20f8f49c8e
commit 71517ef506
+20 -13
View File
@@ -7,10 +7,13 @@
## 1. start() vs run()
### Output:
-Calling run()
-Running in: main
-Calling start()
-Running in: Thread-2
Calling run()
Running in: main
Calling start()
Running in: Thread-2
### Explanation
When calling "t1.run()" directly, the run method executes in the current thread (main).
@@ -30,24 +33,28 @@ When calling "t2.start()" , java creates a new thread and executes run() in that
### Output(with demon):
-Main thread ends
-Deamon thread running...
-(Program terminates quickly)
Main thread ends
Deamon thread running...
(Program terminates quickly)
### Output(without demon):
-Main thread ends.
-Deamon thread running...
-(20 times)
Main thread ends.
Deamon thread running...
(20 times)
### Explanation
-A daemon thread is a background thread that does NOT prevent the JVM from exiting.
A daemon thread is a background thread that does NOT prevent the JVM from exiting.
When the main thread (non-daemon) finishes,
the JVM checks if there are any non-daemon threads still running.
Since only the daemon thread remains,
the JVM terminates immediately without waiting for the daemon thread to complete its 20 iterations.
-Without setDaemon(true), the thread becomes a user thread (non-daemon) .
Without setDaemon(true), the thread becomes a user thread (non-daemon) .
User threads prevent the JVM from exiting until they complete.
Therefore, the JVM waits for the thread to finish all 20 iterations before terminating the program.
@@ -65,7 +72,7 @@ Therefore, the JVM waits for the thread to finish all 20 iterations before termi
### Output:
-Thread is running using a...!
Thread is running using a...!
### What is `() -> {}`?
This is a **Lambda Expression** introduced in Java.