redmi2 + multithreading
This commit is contained in:
@@ -7,10 +7,13 @@
|
|||||||
## 1. start() vs run()
|
## 1. start() vs run()
|
||||||
### Output:
|
### Output:
|
||||||
|
|
||||||
-Calling run()
|
Calling run()
|
||||||
-Running in: main
|
|
||||||
-Calling start()
|
Running in: main
|
||||||
-Running in: Thread-2
|
|
||||||
|
Calling start()
|
||||||
|
|
||||||
|
Running in: Thread-2
|
||||||
|
|
||||||
### Explanation
|
### Explanation
|
||||||
When calling "t1.run()" directly, the run method executes in the current thread (main).
|
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):
|
### Output(with demon):
|
||||||
|
|
||||||
-Main thread ends
|
Main thread ends
|
||||||
-Deamon thread running...
|
|
||||||
-(Program terminates quickly)
|
Deamon thread running...
|
||||||
|
|
||||||
|
(Program terminates quickly)
|
||||||
|
|
||||||
### Output(without demon):
|
### Output(without demon):
|
||||||
|
|
||||||
-Main thread ends.
|
Main thread ends.
|
||||||
-Deamon thread running...
|
|
||||||
-(20 times)
|
Deamon thread running...
|
||||||
|
|
||||||
|
(20 times)
|
||||||
|
|
||||||
### Explanation
|
### 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,
|
When the main thread (non-daemon) finishes,
|
||||||
the JVM checks if there are any non-daemon threads still running.
|
the JVM checks if there are any non-daemon threads still running.
|
||||||
Since only the daemon thread remains,
|
Since only the daemon thread remains,
|
||||||
the JVM terminates immediately without waiting for the daemon thread to complete its 20 iterations.
|
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.
|
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.
|
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:
|
### Output:
|
||||||
|
|
||||||
-Thread is running using a...!
|
Thread is running using a...!
|
||||||
|
|
||||||
### What is `() -> {}`?
|
### What is `() -> {}`?
|
||||||
This is a **Lambda Expression** introduced in Java.
|
This is a **Lambda Expression** introduced in Java.
|
||||||
|
|||||||
Reference in New Issue
Block a user