Vernacular Java daemon thread

Source: Internet
Author: User

Onecoder (Bitter force Coder) original, reproduced please be sure to indicate the source: http://www.coderli.com/archives/daemon-thread-plain-words/

About "vernacular": accidental thought of words, perhaps one day can become a series. The purpose is to tell you everything I know in a concise, crisp language.

Threads in Java are divided into two categories, the user thread and the daemon thread.
    1. Thread commonthread = new Thread ("Common thread");

This is the user thread.

    1. Thread daemonthread = new Thread ("Daemon thread");
    2. Daemonthread.setdaemon (true);

This is the daemon thread.

From the "guardian" thread so beautiful name, nature to play the role of "guardian". It's like a man guarding a sister.

The role of the daemon thread, according to the online statement is:The daemon thread is used to serve the user thread, and if no other user thread is running, there is no service object and there is no reason to go on. The white girl is not, the man also killed himself. Write a few examples in a few cases, and you'll know when you run.
    • Two girls-do not want to dry, you hang yours, I hung my

  1. /**
  2. * Test the situation of two user threads
  3. *
  4. * @author Lihzh (Onecoder)
  5. * @date 2012-6-25 10:07:16
  6. */
  7. private static void Twocommonthread () {
  8. String girlonename = "Girl One";
  9. Thread girlone = new Thread (new Myrunner (, girlonename), girlonename);
  10. String girltwoname = "Girl";
  11. Thread girltwo = new Thread (new Myrunner (girltwoname), girltwoname);
  12. Girlone.start ();
  13. System.out.println (Girlonename + "is starting.");
  14. Girltwo.start ();
  15. System.out.println (Girltwoname + "is starting");
  16. }
  17. private static class Myrunner implements Runnable {
  18. private long sleepperiod;
  19. private String threadname;
  20. Public Myrunner (long sleepperiod, String threadname) {
  21. this.sleepperiod = sleepperiod;
  22. this.threadname = threadname;
  23. }
  24. @Override
  25. public Void Run () {
  26. try {
  27. Thread.Sleep (Sleepperiod);
  28. } catch (Interruptedexception e) {
  29. E.printstacktrace ();
  30. }
  31. System.out.println (ThreadName + "has finished.");
  32. }
  33. }

At first, all alive.

3 seconds later, Sister 1 Hung up, Sister 2 Live Well, her life is 5 seconds.

    • A sister and a prince
  1. /**
  2. * Test a User A daemon thread
  3. *
  4. * @author Lihzh (Onecoder)
  5. * @date 2012-6-25 10:22:58
  6. */
  7. Private static void Onecommononedaemonthread () {
  8. String girlname = "Girl";
  9. Thread girl = new Thread (new Myrunner (, girlname), girlname);
  10. String princename = "Prince";
  11. Thread Prince = new Thread (new Myrunner (princename), princename);
  12. Girl.start ();
  13. System.out.println (Girlname + "is starting.");
  14. Prince.setdaemon (true);
  15. Prince.start ();
  16. SYSTEM.OUT.PRINTLN (Prince + "is starting");
  17. }

Start a happy life, sister can live 3 seconds, the prince could have lived 5 seconds.

But 3 seconds later, the sister hung up, the Prince also xunqing.

You might ask, if the Prince lived for 3 seconds, she would live for 5 seconds. I can only say, although you are the prince, the hanging also have to hang, sister will find other high-rich handsome, understand?

Look, the prince has already hung up.

    • Two a prince
  1. /** 
  2. * Test Two daemon threads
  3. *
  4. * @author Lihzh (Onecoder)
  5. * @date 2012-6-25 10:29:18
  6. */
  7. Private static void Twodaemonthread () {
  8. String princeonename = "Prince One";
  9. Thread princeone = new Thread (new Myrunner (princeonename), princeonename);
  10. String princetwoname = "Prince";
  11. Thread princetwo = new Thread (new Myrunner (, princetwoname), princetwoname);
  12. Princeone.setdaemon (true);
  13. Princeone.start ();
  14. System.out.println (Princeonename + "is starting.");
  15. Princetwo.setdaemon (true);
  16. Princetwo.start ();
  17. System.out.println (Princetwoname + "is starting");
  18. }

I can only say, no sister, there is no reason to live, directly hung up. Now, do you know the daemon thread?

Vernacular Java daemon thread

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.