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.
- Thread commonthread = new Thread ("Common thread");
This is the user thread.
- Thread daemonthread = new Thread ("Daemon thread");
- 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
- /**
- * Test the situation of two user threads
- *
- * @author Lihzh (Onecoder)
- * @date 2012-6-25 10:07:16
- */
- private static void Twocommonthread () {
- String girlonename = "Girl One";
- Thread girlone = new Thread (new Myrunner (, girlonename), girlonename);
- String girltwoname = "Girl";
- Thread girltwo = new Thread (new Myrunner (girltwoname), girltwoname);
- Girlone.start ();
- System.out.println (Girlonename + "is starting.");
- Girltwo.start ();
- System.out.println (Girltwoname + "is starting");
- }
- private static class Myrunner implements Runnable {
- private long sleepperiod;
- private String threadname;
- Public Myrunner (long sleepperiod, String threadname) {
- this.sleepperiod = sleepperiod;
- this.threadname = threadname;
- }
- @Override
- public Void Run () {
- try {
- Thread.Sleep (Sleepperiod);
- } catch (Interruptedexception e) {
- E.printstacktrace ();
- }
- System.out.println (ThreadName + "has finished.");
- }
- }
At first, all alive.
3 seconds later, Sister 1 Hung up, Sister 2 Live Well, her life is 5 seconds.
- /**
- * Test a User A daemon thread
- *
- * @author Lihzh (Onecoder)
- * @date 2012-6-25 10:22:58
- */
- Private static void Onecommononedaemonthread () {
- String girlname = "Girl";
- Thread girl = new Thread (new Myrunner (, girlname), girlname);
- String princename = "Prince";
- Thread Prince = new Thread (new Myrunner (princename), princename);
- Girl.start ();
- System.out.println (Girlname + "is starting.");
- Prince.setdaemon (true);
- Prince.start ();
- SYSTEM.OUT.PRINTLN (Prince + "is starting");
- }
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.
- /**
- * Test Two daemon threads
- *
- * @author Lihzh (Onecoder)
- * @date 2012-6-25 10:29:18
- */
- Private static void Twodaemonthread () {
- String princeonename = "Prince One";
- Thread princeone = new Thread (new Myrunner (princeonename), princeonename);
- String princetwoname = "Prince";
- Thread princetwo = new Thread (new Myrunner (, princetwoname), princetwoname);
- Princeone.setdaemon (true);
- Princeone.start ();
- System.out.println (Princeonename + "is starting.");
- Princetwo.setdaemon (true);
- Princetwo.start ();
- System.out.println (Princetwoname + "is starting");
- }
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