39 lines
1.1 KiB
Markdown
39 lines
1.1 KiB
Markdown
# Answers of theoretical questions
|
|
|
|
## 1:
|
|
|
|
**outputs:**
|
|
|
|
Calling run()
|
|
Running in: main
|
|
Calling start()
|
|
Running in: Thread-2
|
|
|
|
this is because of the difference between run() and start() method.
|
|
when you call run method, it only calls the run function of StartVsRun class and it runs on the main thread;
|
|
but when you call **start** method it runs on new thread(here we name it Thread-2)
|
|
---
|
|
## 2:
|
|
|
|
**outputs:**
|
|
|
|
Main thread ends.
|
|
Daemon thread running...
|
|
|
|
if we set daemon true The **JVM** terminates the thread right after the main thread ends.
|
|
if we don't set the thread as a daemon, after the main thread ended it will continue it's work until it will be done(print Daemon thread running 20 times).
|
|
|
|
|
|
### Real-Life use case of daemon threads:
|
|
|
|
- Memory and Resource Management(like java's garbage collector)
|
|
- Infrastructure & Telemetry Monitoring(like send a ping to a central server every 5 seconds)
|
|
- Spell Checkers
|
|
|
|
## 3:
|
|
|
|
**output:**
|
|
|
|
Thread is running using a ...!
|
|
|
|
it calls **Lambda Expression** and it is shorter and faster way than making a class which extend thread or implement runnable to have another thread for our works. |