1. The difference between a user thread and a daemon thread
Both the user thread and the daemon thread are threads, except that the program ends when the Java virtual machine dead on all user threads. Regardless of whether or not the daemon is still running, if the daemon is still running, it will end immediately. It is well understood that the daemon thread is used to assist the user thread, such as the company's security and staff, and when the employees have left, the security will be off.
2. Applicable scenarios for user threads and daemon threads
By the difference between the two and dead time, the daemon thread is not suitable for input and output or calculation, because the user thread execution, the program is dead, suitable for the scene of the secondary user thread, such as the JVM garbage collection, memory management is the daemon thread, and in the database application, Using the database connection pool, the connection pool itself also contains a lot of background threads, listening to the number of connections, time-out, state and so on.
3. Create a daemon thread
Call the Thread object's method Setdaemon (true) to set the thread as the daemon thread.
1) Thread.setdaemon (true) must be set before Thread.Start ().
2) The new thread generated in the daemon thread is also daemon.
3) Not all applications can be assigned to daemon threads for service, such as read-write operations or computational logic. The virtual machine may have exited because the daemon thread has not yet been able to operate.
4.Java daemon thread and Linux daemon
The two are not a concept. The Linux daemon is a background service process with no console.
In Windows, you can run JAVAW to achieve the purpose of releasing the console, under Unix you add & at the end of the command line. So daemons are not necessarily needed.
Java User threads and daemon threads