Grails:后台线程插件
我有一个疑问,执行完成后是否需要删除后台线程?或者线程被自动清理并删除?
I have a doubt, Do I need to delete a background thread when the execute is done? Or the thread is automatically clean and deleted?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Java 和 Groovy 中,当不再引用对象实例时,它们会自动被垃圾回收。
一旦执行完毕,后台线程插件就不会保存对 Runnable 或传递给它的闭包(我们称它们为“线程”)的引用。
但是,您不应该在类级别的实例变量中保存对这些“线程”的大量引用。 (我想,无论如何,您都不会这样做。)请记住,Grails 中的服务是单例范围的,并且与 Grails 应用程序具有相同的生命周期。
相反,方法级别的对象实例在方法调用完成后被垃圾收集。
毕竟,根本不需要删除“线程”,只需不要将它们存储在实例变量中即可。
In Java and Groovy, object instances are automatically garbage-collected when there's no more reference to them.
The Background Thread Plugin doesn't hold references to
Runnable
s or closures you pass to it (let's call them "threads") once those have been executed.However, you shouldn't hold large arrays of references to these "threads" in instance variables at the class level. (I guess, you wouldn't do, anyway.) Remember that Services in Grails are singleton-scoped and have the same life span as the Grails application.
In contrast, object instances at the method level are garbage-collected after the method call has completed.
After all, there's simply no need to delete "threads", just don't store the them in instance variables.