Java multithreading-daemon)

Source: Internet
Author: User
Tags exit in

Daemon description

Java threads can be marked as daemon threads ). The main difference between daemon and non-daemon threads is their impact on Java program exit. Java programs exit in the following situations:
The Runtime. exit method is called.
All non-daemon threads have ended. Even if a daemon thread exists at this time, the Java program will exit.
When a thread is created, if its parent thread is a daemon thread, it automatically becomes a daemon thread. If the parent thread is not a daemon thread, you can call setDaemon to set the thread as a daemon before starting the thread. Note that it must be called before startup; otherwise, an exception is thrown:
Java code
If (isAlive ()){
Throw new IllegalThreadStateException ();
}
Use of daemon threads

The daemon thread is usually used to execute some background jobs, such as playing background music when your application is running, performing automatic syntax check and saving in the text editor. Java garbage collection is also a daemon thread. The advantage of the daemon thread is that you don't need to worry about its termination. For example, if you want to play background music when your application is running, if you set the background music playing thread to a non-daemon thread, when the user requests to exit, you must not only exit the main thread, but also notify the background music playing thread to exit. If it is set to a daemon thread, it is not required.
 
The following content is from: oreilly.com -- threading ing Java: Threads
Daemon threads are primarily useful in standalone Java applications and in the implementation of the Java system itself, but not in applets. since an applet runs inside of another Java application, any daemon threads it creates will continue to live until the controlling application exits -- probably not the desired effect.
 
The daemon thread is mainly used to implement independent Java applications and Java systems. The applet does not need to use a daemon thread, because the applet itself runs in another Java application, and any daemon thread will survive until the control program exits and the control program exits, no matter whether you create a daemon thread or a non-daemon thread, it will automatically exit. This is not the effect that the daemon thread wants, or the same effect can be achieved without a daemon thread.

Author: "Silent Lake"
 

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.