Module: | Arrays, Strings & Exception Handling
Q56: Consider the following statements regarding Daemon threads and User threads:
1. Daemon threads are specialized, low-priority background threads primarily utilized for automated system maintenance tasks such as Garbage Collection.
2. The Java Virtual Machine will completely shut down and terminate the application the precise moment all Daemon threads finish executing, regardless of whether User threads are still actively running.
3. To successfully convert a standard User thread into a Daemon thread, the developer must strictly invoke the setDaemon(true) method before invoking the start() method.
Which of the above statements is/are correct?
2. The Java Virtual Machine will completely shut down and terminate the application the precise moment all Daemon threads finish executing, regardless of whether User threads are still actively running.
3. To successfully convert a standard User thread into a Daemon thread, the developer must strictly invoke the setDaemon(true) method before invoking the start() method.
Which of the above statements is/are correct?
✅ Correct Answer: B
🎯 Quick Answer:
The correct combination is 1 and 3. Statement 2 is incorrect because the shutdown logic is precisely the reverse. The JVM terminates the application the moment all User threads finish executing. It violently kills any remaining Daemon threads instantly, regardless of what operation they are currently performing.User threads (like the main thread) execute the primary application logic and keep the JVM alive.
Daemon threads are service providers that operate silently in the background.
Structural Breakdown: Any thread spawned by the main thread is automatically a User thread by default.
The developer can alter this status using `thread.setDaemon(true)`. Historical/Related Context: Daemon threads are incredibly useful for background tasks like auto-saving user data in a text editor or pinging a server to maintain a connection.
Because they are Daemon threads, the developer does not have to write complex logic to shut them down when the user clicks "Exit"; the JVM automatically purges them when the main interface thread terminates.
Causal Reasoning: The JVM mandates that setDaemon(true) must be called before start() (Statement 3). Once a thread has been registered with the host Operating System and is actively executing in the RUNNABLE state, its core identity is locked.
Attempting to change a running User thread into a Daemon thread will instantly trigger an IllegalThreadStateException.