Created
November 2, 2013 15:47
-
-
Save zxh/7280252 to your computer and use it in GitHub Desktop.
What is Daemon thread in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I answered it on stackoverflow.com: | |
http://stackoverflow.com/questions/2213340/what-is-daemon-thread-in-java/18428894#18428894 | |
Java has a special kind of thread called daemon thread. | |
Very low priority. | |
Only executes when no other thread of the same program is running. | |
JVM ends the program finishing these threads, when daemon threads are the only threads running in a program. | |
What does daemon thread used for? | |
Normally used as service providers for normal threads. Usually have an infinite loop that waits for the service request or performs the tasks of the thread. They can’t do important jobs. (Because we don't know when they are going to have CPU time and they can finish any time if there aren't any other threads running. ) | |
A typical example of these kind of threads is the Java garbage collector. | |
There's more... | |
You only call the setDaemon() method before you call the start() method. Once the thread is running, you can’t modify its daemon status. | |
Use isDaemon() method to check if a thread is a daemon thread or a user thread. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment