守护程序线程服务实用程序

时间:2012-07-26 16:03:59

标签: java multithreading

  

可能重复:
  Regarding daemon thread providing some service to non daemon thread

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

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();
    }
}

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

0 个答案:

没有答案