Daemon thread
The core difference between user thread and daemon thread is that the JVM will only shut down a program when all user threads have terminated. Daemon threads are terminated by the JVM when there are no longer any user threads running, including the main thread of execution.
Daemon threads are supposed to be used in cases where the thread can safely be terminated without negative consequences, especially in repetitive or background situations, which is why garbage collection is considered a Daemon thread.
Example: Think of the JVM as a club. Daemon threads are like the bouncers and User threads are the people. Until everyone's gone, the bouncers will stay. But once they leave, the bouncers aren't needed anymore and they leave too. That's what happens in the JVM. User threads stay until they're done, and once they're done, the JVM terminates any remaining Daemon threads.
Bottom line: The JVM won't terminate a User thread, but it couldn't care less about Daemon threads
Daemon threads are supposed to be used in cases where the thread can safely be terminated without negative consequences, especially in repetitive or background situations, which is why garbage collection is considered a Daemon thread.
Example: Think of the JVM as a club. Daemon threads are like the bouncers and User threads are the people. Until everyone's gone, the bouncers will stay. But once they leave, the bouncers aren't needed anymore and they leave too. That's what happens in the JVM. User threads stay until they're done, and once they're done, the JVM terminates any remaining Daemon threads.
Bottom line: The JVM won't terminate a User thread, but it couldn't care less about Daemon threads

0 Comments:
Post a Comment
<< Home