package com.diguage.thinking.concurrency;

import java.util.concurrent.TimeUnit;

/**
 * User: D瓜哥，http://www.diguage.com/
 * Date: 13-8-30
 * Time: 上午11:41
 */
class Daemon implements Runnable {
    private Thread[] t = new Thread[10];

    @Override
    public void run() {
        for (int i = 0; i < t.length; i++) {
            t[i] = new Thread(new DaemonSpawn());
            t[i].start();
            System.out.println("DaemonSpawn " + i + " started. ");
        }
        for (int i = 0; i < t.length; i++) {
            System.out.println("t[" + i + "].isDaemon() = " + t[i].isDaemon() + ". ");
        }
        while (true) {
            Thread.yield();
        }
    }
}

class DaemonSpawn implements Runnable {
    @Override
    public void run() {
        while (true) {
            Thread.yield();
        }
    }
}

public class Daemons {
    public static void main(String[] args) throws InterruptedException {
        Thread d = new Thread(new Daemon());
        d.setDaemon(true);
        d.start();
        System.out.println("d.isDaemon() = " + d.isDaemon() + ". ");

        TimeUnit.SECONDS.sleep(1);

    }

}

@@ -79,16 +79,47 @@
 t;%0A%0A/**%0A
+ * %E6%BC%94%E7%A4%BA%E5%90%8E%E5%8F%B0%E7%BA%BF%E7%A8%8B%E8%A1%8D%E7%94%9F%E5%87%BA%E6%9D%A5%E7%9A%84%E7%BA%BF%E7%A8%8B%E8%BF%98%E6%98%AF%E5%90%8E%E5%8F%B0%E7%BA%BF%E7%A8%8B%0A * %3Cp/%3E%0A
  * User:

添加注释“演示后台线程衍生出来的线程还是后台线程”