Java 中的 Executor 和 Daemon
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1516172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executor and Daemon in Java
提问by Ittai
I have a MyThread object which I instantiate when my app is loaded through the server, I mark it as a Daemon thread and then call start()on it. The thread is meant to sit and wait for information from a queue as long as the application is active.
My problem/question is this: Currently MyThread is extending Thread because I mark it as Daemon and I read about how it's more prefferable to implement Runnable and to use Executors. So what I wanted to ask is if MyThread will implement Runnable instead of extending Thread (and of course will be renamed) and I'll use newSingleThreadScheduledExecutor()how, what or maybe where, do I mark something as Daemon.
I hope I haven't made a mess of terms, please excuse me if I have as some parts of the multithreading environment are very new to me.
我有一个 MyThread 对象,当我的应用程序通过服务器加载时,我会实例化它,我将它标记为守护线程,然后调用start()它。只要应用程序处于活动状态,该线程就会坐下来等待来自队列的信息。我的问题/问题是:目前 MyThread 正在扩展 Thread,因为我将其标记为 Daemon,并且我阅读了有关实现 Runnable 和使用 Executors 的更多信息。所以我想问的是,MyThread 是否会实现 Runnable 而不是扩展 Thread(当然会被重命名),我将使用newSingleThreadScheduledExecutor()如何、什么或可能在哪里将某些东西标记为 Daemon。我希望我没有把术语弄得一团糟,请原谅我,因为多线程环境的某些部分对我来说很陌生。
Thanks Ittai
谢谢一泰
Update:
The module I'm referring to in my app is a web-app which has a few threads actually of this sort and what they do have in common is that they all in the ServletContextas a member for various reasons. Currently I extend Threadto WebThreadwhich has the ServletContextas a memebr and all subclasses can utilize this. If I switch over to the Runnable paradigm with the Executor and ThreadFactory than basically I need to have an ugly hybrid of WebRunnablewhich implements Runnableand has the ServletContextas a public member and have my ThreadFactoryimplement newThread(WebRunnable arg0)in addition to newThread(Runnable arg0).
I'm not sure what's best.
Thanks
更新:我在我的应用程序中所指的模块是一个网络应用程序,它实际上有一些此类线程,它们的共同点是ServletContext出于各种原因,它们都作为成员加入。目前我扩展Thread到WebThread具有ServletContext作为 memebr 的所有子类都可以使用它。如果我切换到与执行人及的ThreadFactory了Runnable范式比基本上我需要有一个丑陋的混合动力车WebRunnable,它实现Runnable并具有ServletContext作为公共成员,并且对我的ThreadFactory落实newThread(WebRunnable arg0)除了newThread(Runnable arg0)。我不确定什么是最好的。谢谢
回答by Stu Thompson
Check out the JavaDoc for newSingleThreadScheduledExecutor(ThreadFactory threadFactory)
查看 JavaDoc newSingleThreadScheduledExecutor(ThreadFactory threadFactory)
It would be implemented something like this:
它将被实现如下:
public class MyClass {
private DaemonThreadFactory dtf = new DaemonThreadFactory();
private ScheduledExecutorService executor =
Executors.newSingleThreadScheduledExecutor(dtf);
// ....class stuff.....
// ....Instance the runnable.....
// ....submit() to executor....
}
class DaemonThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
}
回答by Brian Agnew
If you're using a scheduled executor, you can provide a ThreadFactory. This is used to create new Threads, and you can modify these (e.g. make them daemon) as you require.
如果您使用的是预定的执行程序,则可以提供ThreadFactory。这用于创建新线程,您可以根据需要修改它们(例如,使它们成为守护进程)。
EDIT: To answer your update, your ThreadFactoryjust needs to implement newThread(Runnable r)since your WebRunnableis aRunnable. So no real extra work.
编辑:要回答您的更新,您ThreadFactory只需要实施,newThread(Runnable r)因为您WebRunnable是Runnable. 所以没有真正的额外工作。
回答by Johan Sj?berg
Just to complement with another possible solution for completeness. It may not be as nice though.
只是为了补充另一个可能的完整性解决方案。不过可能没那么好。
final Executor executor = Executors.newSingleThreadExecutor();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
executor.shutdownNow();
}
});

