Java User threads and daemon threads

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.