answer theoric question
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
# Theoretical Questions
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Question 1
|
||||||
|
### 1. `start()` vs `run()`
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
When we use the run method, we are actually in the main thread where the run function is called and it is executed in the same main thread, meaning there is a total of 1 thread. But when we use the start method, we actually have 2 threads, where a separate thread is created and the instructions inside the run function are executed in the new thread.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
## Question 2
|
||||||
|
### 2. Daemon Threads
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
1. Since we have the Daemon state in this thread, the program terminates the daemon thread after the main thread finishes, and the desired thread cannot perform its action 20 times.
|
||||||
|
|
||||||
|
2. if we remove `thread.setDaemon(true)` this thread 20 times print "Daemon thread running..." and then program finished.
|
||||||
|
|
||||||
|
3. In real life : for example when we turn off the car . the thread that play song from radio should be killed
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
## Question 3
|
||||||
|
### 3.A shorter way to create threads
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
This syntax call `Lambda Expression`
|
||||||
|
|
||||||
|
when we use Lambda expression we will write less code, and we don't need to create a new class
|
||||||
|
but when we use Traditional model, our code is cleaner than other model, but we should write more code
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 1.5 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 918 B |
Reference in New Issue
Block a user