【发布时间】:2012-07-26 14:31:59
【问题描述】:
我有以下代码..
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 --->true
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
[two] - Hello World.
请告知上面这段代码在做什么..因为我想要实现的是将一个线程设置为守护进程,然后该守护线程将为非守护线程提供服务!!
【问题讨论】:
-
嗯,它看起来已经在做你想做的事了。你能更清楚你真正要问的是什么吗?
-
您是否查看过 java.util.concurrent 中的类的 API 文档?-
-
您正在创建十个线程。线程之间似乎没有区别 - 它们是根据
Runnable代码的相同定义创建的,并且没有使用不同的变量进行初始化。
标签: java multithreading