Java Multithreading----Note points about join methods and a summary of the daemon threads

Source: Internet
Author: User
Tags garbage collection

About Join Method: Join method must be after start, that is, first thread ready, then join, otherwise invalid. Note that the Join method simply allows two threads to execute synchronously, still two threads, not merged into one.


Java Daemon Threads
1, what is the daemon thread (daemon thread).
The daemon thread is a background-running thread, in contrast to the user thread (the users thread). It belongs to the thread that created it, so when the thread that created the guard ends, the daemon dies. (The daemon, if it has its own extinction logic, can die before the daemon) when the running thread is a daemon, the Java virtual machine exits
2, how to create a daemon thread.
Creates a new thread, using the Setdaemon (Boolean on) method to easily set the daemon mode of the thread, true to daemon mode, and false to user mode. the Setdaemon (Boolean on) method must be invoked before the thread is started (that is, before start ()), and an exception is generated when the threads are running. The Isdaemon method tests whether the thread is a daemon thread. It is worth mentioning that when you generate other threads in a daemon, the newly generated threads will be the daemon (and guardian of the same user thread) without setting the Daemon property, as are the user threads.
3, the use of the Guardian thread.
The daemon thread uses less, but is not useless, for example, the JVM's garbage collection, memory management, and other threads are daemon threads. There is in doing database application, the use of the database connection pool, the connection pool itself also contains a lot of background threads, monitoring the number of connections, timeout time, state and so on.
The daemon thread is typically serviced for user threads.
4, the difference between the daemon thread and the user thread.
The user thread has its own lifecycle and does not depend on the thread that created it. The user thread can continue to run after the thread that created it has ended. Threads that run for a limited period of time are typically user threads.
The daemon dies at the end of the created thread. Threads that run indefinitely should usually be defined as daemons, just because you need a way to end them.

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.