80 lines
3.1 KiB
Markdown
80 lines
3.1 KiB
Markdown
Question 1: start() vs run()
|
||
|
||
What output do you get from the program? Why?
|
||
output:
|
||
|
||
Calling run()
|
||
Running in: main
|
||
Calling start()
|
||
Running in: Thread-2
|
||
|
||
When we call the run() method, no new thread is created, and the code is executed within the main thread. However, when we call the start() method, the JVM creates a new thread and executes the run() method inside that thread.
|
||
|
||
What’s the difference in behavior between calling start() and run()?
|
||
|
||
The run() method does not create a new thread and executes in the current thread, while the start() method creates a new thread and runs the task concurrently in that thread.
|
||
|
||
Question 2: Daemon Threads
|
||
Code
|
||
|
||
What output do you get from the program? Why?
|
||
|
||
outpot:
|
||
Main thread ends.
|
||
|
||
sometime before end of program: Daemon thread running...
|
||
|
||
The reason is that the thread we created is a Daemon thread. Daemon threads are used for background tasks and do not prevent the JVM from shutting down. As soon as the main thread finishes, the JVM terminates, and all Daemon threads are stopped as well.
|
||
|
||
What happens if you remove thread.setDaemon(true)?
|
||
|
||
If we remove thread.setDaemon(true), the created thread will no longer be a Daemon thread and will become a regular (User) thread.
|
||
|
||
In this case, the JVM waits for the thread to finish its execution before shutting down. Therefore, the message:
|
||
|
||
Daemon thread running...
|
||
|
||
will be printed 20 times, and then the program will terminate.
|
||
|
||
What are some real-life use cases of daemon threads?
|
||
|
||
Daemon threads are usually used for background tasks, such as:
|
||
|
||
Garbage Collection in the JVM.
|
||
Logging.
|
||
Auto-saving files.
|
||
Monitoring system status.
|
||
Cleaning temporary cache data.
|
||
Checking the status of networks and services.
|
||
|
||
These tasks help the program run more efficiently, but they are not important enough to keep the application alive. Therefore, once all user threads have finished, the JVM can shut down without waiting for daemon threads to complete.
|
||
|
||
Question 3: A shorter way to create threads
|
||
Code
|
||
|
||
What output do you get from the program?
|
||
|
||
output:Thread is running using a ...!
|
||
|
||
What is the () -> { ... } syntax called?
|
||
|
||
This structure is called a Lambda Expression.
|
||
|
||
A Lambda Expression provides a shorter and more readable way to implement functional interfaces such as Runnable.
|
||
|
||
In this example, the Lambda Expression actually contains the code that would normally be placed inside the run() method. Instead of creating a separate class and implementing run(), we can write the implementation directly using a Lambda Expression.
|
||
|
||
How is this code different from creating a class that extends Thread or implements Runnable?
|
||
|
||
In the traditional approach, we had to create a separate class that either extends Thread or implements Runnable. This makes the code longer and more verbose.
|
||
|
||
By using a Lambda Expression, we can achieve the same result with much shorter and cleaner code.
|
||
|
||
Advantages of Lambda Expressions:
|
||
|
||
Less code
|
||
Better readability
|
||
Easier maintenance
|
||
Suitable for short and simple thread tasks
|
||
|
||
However, there is no performance difference between a Lambda Expression and a regular Runnable implementation. The main benefit is simpler and more readable code. |