我需要清理Java中的Thread对象吗? [英] Do I need to clean up Thread objects in Java?
问题描述
在我的Java应用程序中,我有一个Runnable,例如:
In my Java application I have a Runnable such as:
this.runner = new Runnable({
@Override
public void run() {
// do something that takes roughly 5 seconds.
}
});
我大约需要每30秒在一个单独的线程中运行一次(尽管这可能有所不同).代码的性质使我可以运行它,而不必理会它(成功还是失败).我在我的应用程序中按以下一行代码进行操作:
I need to run this roughly every 30 seconds (although this can vary) in a separate thread. The nature of the code is such that I can run it and forget about it (whether it succeeds or fails). I do this as follows as a single line of code in my application:
(new Thread(this.runner)).start()
现在,这很好.但是,我想知道每个线程实例完成运行后是否应该进行某种清理?我正在 VisualVM
中对该应用程序进行CPU性能分析,并且可以看到,在1小时的运行时间过程中,正在创建许多线程.这种担忧有效吗?还是一切正常?
Now, this works fine. However, I'm wondering if there is any sort of cleanup I should be doing on each of the thread instances after they finish running? I am doing CPU profiling of this application in VisualVM
and I can see that, over the course of 1 hour runtime, a lot of threads are being created. Is this concern valid or is everything OK?
我启动 new Thread
而不是简单地将 this.runner
定义为 Thread
的原因是,我有时需要运行 this.runner
同时进行两次(在第一次运行调用完成之前),如果我将 this.runner
定义为 Thread
,则无法执行此操作单个 Thread
对象只能在初始执行完成后再次运行.
N.B. The reason I start a new Thread
instead of simply defining this.runner
as a Thread
, is that I sometimes need to run this.runner
twice simultaneously (before the first run call has finished), and I can't do that if I defined this.runner
as a Thread
since a single Thread
object can only be run again once the initial execution has finished.
推荐答案
使用后需要清理"或关闭"的Java对象通常实现 try-with-resources 进行清理变得容易. 线程
类未实现 AutoCloseable
,并且没有"close"或"dispose"方法.因此,您无需进行任何显式清理.
Java objects that need to be "cleaned up" or "closed" after use conventionally implement the AutoCloseable
interface. This makes it easy to do the clean up using try-with-resources. The Thread
class does not implement AutoCloseable
, and has no "close" or "dispose" method. So, you do not need to do any explicit clean up.
但是
(new Thread(this.runner)).start()
不能保证立即开始计算 Runnable
.您可能不在乎它是成功还是失败,但是我想您 do 根本不在乎它是否运行.您可能希望限制同时运行的这些任务的数量.例如,您可能只希望一次运行.因此,您可能想 join()
线程(或者也许是
is not guaranteed to immediately start computation of the Runnable
. You might not care whether it succeeds or fails, but I guess you do care whether it runs at all. And you might want to limit the number of these tasks running concurrently. You might want only one to run at once, for example. So you might want to join()
the thread (or, perhaps, join with a timeout). Joining the thread will ensure that the thread will completes its computation. Joining the thread with a timeout increases the chance that the thread starts its computation (because the current thread will be suspended, freeing a CPU that might run the other thread).
但是,不建议创建多个线程来执行常规或频繁任务.您应该改为线程池.这将使您能够控制最大的并发量,并可以为您提供其他好处(例如,确定不同任务的优先级),并分摊
However, creating multiple threads to perform regular or frequent tasks is not recommended. You should instead submit tasks to a thread pool. That will enable you to control the maximum amount of concurrency, and can provide you with other benefits (such as prioritising different tasks), and amortises the expense of creating threads.
您可以配置线程池以使用固定长度( bounded )任务队列,并导致将线程提交到 ThreadPool.execute(Runnable)
说
You can configure a thread pool to use a fixed length (bounded) task queue and to cause submitting threads to execute submitted tasks itself themselves when the queue is full. By doing that you can guarantee that tasks submitted to the thread pool are (eventually) executed. The documentation of ThreadPool.execute(Runnable)
says it
在将来的某个时间执行给定的任务
Executes the given task sometime in the future
这表明该实现可以保证它最终将运行所有已提交的任务 ,即使您不执行这些特定任务来确保已执行提交的任务.
which suggests that the implementation guarantees that it will eventually run all submitted tasks even if you do not do those specific tasks to ensure submitted tasks are executed.
这篇关于我需要清理Java中的Thread对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!