问题描述
如何将Callable线程作为守护线程?
How can I make Callable thread as daemon thread?
这是我正在尝试的。我试图执行一组线程,其中一个线程没有完成并进入无限循环。它的作用是即使执行了所有代码语句,程序的主线程也不会终止。之后主线程进入暂停模式。
Here is what I am trying. I am trying to execute a set of threads of which one of them does not complete and goes into infinite loop. What it does is the main thread of the program does not terminate even though all the code statements are executed. The main thread goes into suspended mode after that.
以下是相同的代码片段。
Here is the code snippet for the same.
public class MyThread implements Callable<String> {
private int value;
public MyThread(int value) {
this.value = value;
}
@Override
public String call() throws Exception {
//Thread.currentThread().setDaemon(true);
System.out.println("Executing - " + value);
if (value == 4) {
for (; ; );
}
return value + "";
}
}
主程序
public class ExecutorMain {
public static String testing() {
ExecutorService executor = null;
List<Future<String>> result = null;
String parsedValue = null;
try {
executor = Executors.newSingleThreadExecutor();
List<MyThread> threads = new ArrayList<MyThread>();
for (int i = 1; i < 10; i++) {
MyThread obj = new MyThread(i);
threads.add(obj);
}
result = executor.invokeAll(threads, Long.valueOf("4000"), TimeUnit.MILLISECONDS);
//result = executor.invokeAll(threads);
for (Future<String> f : result) {
try {
parsedValue = f.get();
System.out.println("Return Value - " + parsedValue);
} catch (CancellationException e) {
System.out.println("Cancelled");
parsedValue = "";
f.cancel(true);
}
}
executor.shutdownNow();
} catch (Exception e) {
System.out.println("Exception while running threads");
e.printStackTrace();
} finally {
List executedThreads = executor.shutdownNow();
System.out.println(executedThreads);
for (Object o : executedThreads) {
System.out.println(o.getClass());
}
}
System.out.println("Exiting....");
//System.exit(1);
return "";
}
public static void main(String[] args) {
testing();
}
}
从我之前关于在Java中悬空线程是我必须将我的线程作为守护线程。
What I got to understand from my earlier question about Dangling threads in Java is that I have to make my threads as daemon threads.
推荐答案
您需要使用新的 ThreadFactory
来创建守护程序线程。请在此处查看此答案:
You need to use a new ThreadFactory
that creates daemon threads. See this answer here: Executor and Daemon in Java
默认情况下,执行程序在构建池时创建非守护程序线程。但是你可以注入你自己的 ThreadFactory
,它会为池创建线程。
By default the executors create non-daemon threads whenever they build their pools. But you can inject your own ThreadFactory
which creates the threads for the pool.
例如:
executor = ExecutorService.newSingleThreadExecutor(new MyThreadFactory());
ThreadFactory
实现 newThread
方法:
Thread newThread(Runnable r)
根据我上面链接的答案复制,你可以实现它:
Copied from the answer I linked to above, you could implement it like:
class MyThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
}
您在问题中提到:
是的,这不起作用,因为一旦线程启动就无法设置守护进程标志。
Yeah, this won't work because you cannot set the daemon flag once the thread has been started.
这篇关于将可调用线程作为守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!