adding Report.md
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
1:
|
||||
the output is Calling run()
|
||||
Running in: main
|
||||
Calling start()
|
||||
Running in: Thread-2
|
||||
|
||||
t1.run() does not start a new thread.
|
||||
It just calls the run() method like a normal method call, and it executes on the current thread.
|
||||
t2.start() creates a new thread and then that new thread executes run(), so current Thread Name becomes Thread-2.
|
||||
|
||||
run() is just a normal method call, so it runs in the current thread.
|
||||
|
||||
start() creates a new thread, and that new thread executes run().
|
||||
|
||||
2:
|
||||
Output:
|
||||
Main thread ends.
|
||||
It may also print:
|
||||
Daemon thread running…
|
||||
a few times, or sometimes not at all.
|
||||
Why:
|
||||
Because the created thread is a daemon thread. Daemon threads run in the background, and when the main thread finishes, the JVM can stop immediately.
|
||||
So the daemon thread may not complete its loop.
|
||||
If thread.setDaemon(true) is removed:
|
||||
Then the thread becomes a user thread (non-daemon thread).
|
||||
and the JVM will wait for it to finish, so "Daemon thread running..." will print many times until the loops end.
|
||||
Real-life uses of daemon threads:
|
||||
Garbage collection
|
||||
Background auto-save
|
||||
Cache cleanup
|
||||
Monitoring or logging services
|
||||
Timer or scheduler tasks running in background
|
||||
3:
|
||||
What output do you get from the program?
|
||||
Thread is running using a …!
|
||||
What is the () -> { ... } syntax called?
|
||||
This is called a lambda expression.
|
||||
|
||||
How is this code different from creating a class that extends Thread or implements Runnable?
|
||||
This is a shorter and cleaner way to write thread code.
|
||||
Instead of creating a separate class, the task is written directly inside the lambda expression.
|
||||
Reference in New Issue
Block a user