1

可能重复:
关于为非守护线程提供某些服务的守护线程

我想开发一个应用程序,其中守护线程将为非守护线程提供服务,我开发了以下代码

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class daemonTest {

    public static void main(String... a) throws Exception {
        ExecutorService service = Executors
                .newSingleThreadExecutor(new ThreadFactory() { // anonmyous class start
                    public Thread newThread(Runnable r) {
                        Thread two = new Thread(r, "two");
                        two.setDaemon(true);
                        System.out.println("two --->" + two.isDaemon());
                        return two;
                    }
                });
        for (int i = 0; i < 10; i++)
            service.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println("[" + Thread.currentThread().getName()
                            + "] - Hello World.");
                    Thread.yield();
                }
            });
        service.shutdown();
    }
}

但是请告知如果我创建一个不是守护线程的新线程,那么名为两个的守护线程将如何为新创建的线程提供服务,最后我想要实现

4

0 回答 0