Java threads Learn to organize--1--daemon threads

Source: Internet
Author: User

1. What is a daemon thread?

  Today, the teacher explained that I know there is a Guardian thread this matter! The original daemon thread often exists in our side, such as: A free web game, there will be more or less some of the inserted ads!!

In general, we do not click on these ads, but advertisers are required to click the amount of Ah! So the people who make the game will secretly use the daemon to access this ad interface, but our users don't know

Of Is that the Guardian thread is generally invisible to the user, visible to the programmer behind it!

Another example: We use the QQ software, we can chat while chatting, while updating the QQ software! When we chat, close QQ, you will find that QQ update operation has been terminated!

In fact: Update QQ operation is a Guardian thread operation!

    

2, the characteristics of the Guardian thread:

When the main thread is finished, the daemon thread will stop!

/********** How to use *****************/
Helpthread ht=New helpthread (); Ht.setdaemon (true); // set the thread as the daemon thread

  

3. Examples:

1  Packagecn.sxt.helpthreads;2 /**3 * Daemon Thread:4  * 5 * Simulation:6 * QQ Chat, while chatting, while using QQ download updates, when the update has not been completed, the end of the chat, the download is also over! 7  * @authorSmall Wind micro-spirit8  *9  */Ten  Public classHelpthreadextendsthread{ One  A      Public voidrun () { -          -          for(inti=0;i<=100;i++){ theSystem.out.println ("Daemon Thread---downloading, progress:" +i+ "%"); -             if(i==100){ -System.err.println ("Download complete ready to install ..."); -             } +         } -          +     } A      Public Static voidMain (string[] args) { at          -Helpthread ht=NewHelpthread (); -          -Ht.setdaemon (true);//set the thread as the daemon thread -              -System.err.println ("Whether the download thread is a daemon thread:" +ht.isdaemon ());//true: is the daemon thread false: not a daemon thread in Ht.start ();  -          for(inti=0;i<=100;i++){ toSystem.out.println (Thread.CurrentThread (). GetName () + "chat ...." +i); +              -         } the          *     } $     Panax Notoginseng}

Operation Result:

1Whether the download thread is a daemon thread:true2Main chat .... 03Main chat .... 14Daemon Thread---downloading, progress: 0%5Main chat .... 26Daemon Thread---downloading, progress: 1%7Main chat .... 38Daemon Thread---downloading, progress: 2%9Main chat .... 4TenDaemon Thread---downloading, progress: 3% OneMain chat .... 5 ADaemon Thread---downloading, progress: 4% -Daemon Thread---downloading, progress: 5% -Main chat .... 6 theDaemon Thread---downloading, progress: 6% -Main chat .... 7 -Daemon Thread---downloading, progress: 7% -Main chat .... 8 +Daemon Thread---downloading, progress: 8% -Main chat .... 9 +Daemon Thread---downloading, progress: 9% AMain chat .... 10 atDaemon Thread---downloading, progress: 10% -Main chat .... 11 -Daemon Thread---downloading, progress: 11% -Main chat .... 12 -Daemon Thread---downloading, progress: 12% -Main chat .... 13 inDaemon Thread---downloading, progress: 13% -Main chat .... 14 toDaemon Thread---downloading, progress: 14% +Main chat .... 15 -Daemon Thread---downloading, progress: 15% theMain chat .... 16 *Daemon Thread---downloading, progress: 16% $Main chat .... 17Panax NotoginsengDaemon Thread---downloading, progress: 17% -Main chat .... 18 theDaemon Thread---downloading, progress: 18% +Main chat .... 19 ADaemon Thread---downloading, progress: 19% theMain chat .... 20 +Daemon Thread---downloading, progress: 20% -Main chat .... 21st $Daemon Thread---downloading, progress: 21% $Main chat .... 22 -Daemon Thread---downloading, progress: 22% -Main chat .... 23 theDaemon Thread---downloading, progress: 23% -Main chat .... 24WuyiDaemon Thread---downloading, progress: 24% theMain chat .... 25 -Daemon Thread---downloading, progress: 25% WuMain chat .... 26 -Daemon Thread---downloading, progress: 26% AboutMain chat .... 27 $Daemon Thread---downloading, progress: 27% -Main chat .... 28 -Daemon Thread---downloading, progress: 28% -Main chat .... 29 ADaemon Thread---downloading, progress: 29% +Main chat .... 30 theDaemon Thread---downloading, progress: 30% -Main chat .... 31 $Daemon Thread---downloading, progress: 31% theMain chat .... 32 theDaemon Thread---downloading, progress: 32% theMain chat .... 33 theDaemon Thread---downloading, progress: 33% -Main chat .... 34 inDaemon Thread---downloading, progress: 34% theMain chat .... 35 theDaemon Thread---downloading, progress: 35% AboutMain chat .... 36 theDaemon Thread---downloading, progress: 36% theMain chat .... 37 theDaemon Thread---downloading, progress: 37% +Main chat .... 38 -Daemon Thread---downloading, progress: 38% theDaemon Thread---downloading, progress: 39%BayiDaemon Thread---downloading, progress: 40% theDaemon Thread---downloading, progress: 41% theDaemon Thread---downloading, progress: 42% -Daemon Thread---downloading, progress: 43% -Daemon Thread---downloading, progress: 44% theDaemon Thread---downloading, progress: 45% theDaemon Thread---downloading, progress: 46% theDaemon Thread---downloading, progress: 47% theDaemon Thread---downloading, progress: 48% -Daemon Thread---downloading, progress: 49% theDaemon Thread---downloading, progress: 50% theDaemon Thread---downloading, progress: 51% theDaemon Thread---downloading, progress: 52%94Daemon Thread---downloading, progress: 53% theDaemon Thread---downloading, progress: 54% theDaemon Thread---downloading, progress: 55% theDaemon Thread---downloading, progress: 56%98Daemon Thread---downloading, progress: 57% AboutDaemon Thread---downloading, progress: 58% -Daemon Thread---downloading, progress: 59%101Daemon Thread---downloading, progress: 60%102Daemon Thread---downloading, progress: 61%103Daemon Thread---downloading, progress: 62%104Daemon Thread---downloading, progress: 63% theDaemon Thread---downloading, progress: 64%106Main chat .... 39107Main chat .... 40108Main chat .... 41109Main chat .... 42 theMain chat .... 43111Main chat .... 44 theMain chat .... 45113Main chat .... 46 theMain chat .... 47 theMain chat .... 48 theMain chat .... 49117Main chat .... 50118Main chat .... 51119Main chat .... 52 -Main chat .... 53121Main chat .... 54122Main chat .... 55123Main chat .... 56124Main chat .... 57 theMain chat .... 58126Main chat .... 59127Main chat .... 60 -Main chat .... 61129Main chat .... 62 theDaemon Thread---downloading, progress: 65%131Daemon Thread---downloading, progress: 66% theDaemon Thread---downloading, progress: 67%133Daemon Thread---downloading, progress: 68%134Daemon Thread---downloading, progress: 69%135Daemon Thread---downloading, progress: 70%136Daemon Thread---downloading, progress: 71%137Daemon Thread---downloading, progress: 72%138Daemon Thread---downloading, progress: 73%139Daemon Thread---downloading, progress: 74% $Daemon Thread---downloading, progress: 75%141Daemon Thread---downloading, progress: 76%142Daemon Thread---downloading, progress: 77%143Daemon Thread---downloading, progress: 78%144Daemon Thread---downloading, progress: 79%145Daemon Thread---downloading, progress: 80%146Main chat .... 63147Daemon Thread---downloading, progress: 81%148Daemon Thread---downloading, progress: 82%149Daemon Thread---downloading, progress: 83% MaxDaemon Thread---downloading, progress: 84%151Daemon Thread---downloading, progress: 85% theDaemon Thread---downloading, progress: 86%153Daemon Thread---downloading, progress: 87%154Daemon Thread---downloading, progress: 88%155Daemon Thread---downloading, progress: 89%156Main chat .... 64157Daemon Thread---downloading, progress: 90%158Main chat .... 65159Daemon Thread---downloading, progress: 91% theMain chat .... 66161Daemon Thread---downloading, progress: 92%162Main chat .... 67163Daemon Thread---downloading, progress: 93%164Main chat .... 68165Daemon Thread---downloading, progress: 94%166Main chat .... 69167Daemon Thread---downloading, progress: 95%168Main chat .... 70169Daemon Thread---downloading, progress: 96% theMain chat .... 71171Daemon Thread---downloading, progress: 97%172Main chat .... 72173Daemon Thread---downloading, progress: 98%174Main chat .... 73175Daemon Thread---downloading, progress: 99%176Main chat .... 74177Daemon Thread---downloading, progress: 100%178Main chat .... 75179 Download complete ready to install ... theMain chat .... 76181Main chat .... 77182Main chat .... 78183Main chat .... 79184Main chat .... 80185Main chat .... 81186Main chat .... 82187Main chat .... 83188Main chat .... 84189Main chat .... 85 theMain chat .... 86191Main chat .... 87192Main chat .... 88193Main chat .... 89194Main chat .... 90195Main chat .... 91196Main chat .... 92197Main chat .... 93198Main chat .... 94199Main chat .... 95 $Main chat .... 96201Main chat .... 97202Main chat .... 98203Main chat .... 99204Main chat .... 100

Java threads Learn to organize--1--daemon threads

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.