Daemon and not caught exception handler

Source: Internet
Author: User
Tags thread class
Daemon: role: Background managers such as you are    Java  written in the editor   write  Word  documents, examples: you knock on the keyboard, this is a   non-daemon thread,  background there is a   Spell check   thread, it's a daemon, he tries not to disturb your writing,  you can do it at the same time, he found that there is a spelling error in the status bar display error, but you can ignore. Like the   Castle, there is a guard   (daemon), there are princes (not guard threads), they can do their own work, but   castle inside the people have moved away from the,  so the guards do not have the meaning of existence. The only difference between a daemon and a normal thread is that when all the threads in the JVM are daemons, the JVM can quit, and if one or more non-daemon threads do not exit. (The above is for normal exit, call System.exit is bound to exit) so the only meaning of Setdeamon (true) is to tell the JVM not to wait for it to quit, to let the JVM like what to quit, and not to worry about it. Usage: 1.thread.setdaemon (TRUE) must be set before Thread.Start (), or it will run out of a illegalthreadstateexception exception 2.  The new thread produced 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 since Daemon thread has not yet come in and is operating. Example://Daemon task to complete file output
import java.io.*; Class Testrunnable implements runnable{public void run () {try{Thread.Sleep (10  
                  00)//The daemon thread blocks 1 seconds after running file F=new file ("Daemon.txt");  
                  FileOutputStream os=new FileOutputStream (f,true);  
           Os.write ("daemon". GetBytes ());  
               catch (IOException E1) {e1.printstacktrace ();  
           catch (interruptedexception E2) {e2.printstacktrace ();  
        }} public class testdemo2{public static void Main (string[] args) throws Interruptedexception {  
        Runnable tr=new testrunnable ();  
                Thread Thread=new thread (TR); Thread.setdaemon (TRUE); Set the daemon thread Thread.Start (); Start the split process} 
Run Result: There is no "daemon" string in file Daemon.txt. But if you put Thread.setdaemon (true); Set the daemon thread annotation, file Daemon.txt is an unhandled exception handler that can be written to the daemon string: First, find out what the check-type exception is, and what is the non-checked exception. The simplest point of judgment is two: 1. A RuntimeException exception inherits from the exception or error, and the inherited self is a check-type exception (of course, RuntimeException is also a exception subclass). 2. Non-checked class exceptions can be used without capture, while the check-type exception must be handled with a try statement block or the exception to the superior method of processing the code must be written to handle it. Body: The thread-run method cannot throw any of the checked exceptions, but unchecked exception checking can cause the thread to terminate. However, you do not need any catch clauses to handle the exceptions that can be propagated. In contrast, before the thread dies, the exception is passed to a processor for an exception that is not caught. The processor must be a class that wants a thread.uncaughtexceptionhandler interface, and this interface has only one method. void Uncaughtexceptionh (thread t,throwable e) can install one processor for any thread using the Setuncaughtexceptionhandler method. One can install a default processor for all threads using the static method of the thread class Setdefaultuncaughtexceptionhandler. The replacement processor can use the log API to send an unhandled exception to a log file. If the default processor is not installed, the default processor is empty. However, if the processor is not installed for a separate thread, the processor at this point is the Threadgroup object for that thread. The logic of the default implementation of the Java.lang.ThreadGroup class Uncaughtexception is as follows: If the parent thread group exists, its Uncaughtexception method is called. If the parent thread group does not exist, but the default processor is specified (as in the following section, as the default handler for the application), the defaults are invoked if the default processor is not set, the error log is written. But if exception is Threadd, Eath instance, ignore. Sample look here: HTTPS://MY.OSCHINA.NET/SHISHAOMENG/BLOG/121740 Reference Blog: https://my.oschina.net/shishaomeng/blog/121740 http://blog.csdn.net/yixiaoqingyuz/article/details/ 4485606 http://bbs.csdn.net/topics/80298851 http://www.cnblogs.com/super-d2/p/3348183.html

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.