简体   繁体   English

线程和垃圾回收

[英]Threads and garbage collection

I have a windows service which runs continuously and creates some threads to do some work. 我有一个连续运行的Windows服务,并创建了一些线程来做一些工作。 I want to make sure that these threads are properly disposed of (garbage collected after they are finished. 我要确保正确处理这些线程(完成后收集的垃圾。

However, I also want to be able to check to see if they are alive periodically and terminate them if they are. 但是,我还希望能够检查它们是否定期存在并终止它们。 I know I can't keep any references to them, though, because then they wouldn't be garbage collected. 我知道我无法保留对它们的任何引用,因为那样的话它们就不会被垃圾收集。

Is there an alternative way to check for the existence/state of user-defined threads? 有没有其他方法可以检查用户定义线程的存在/状态? I was thinking maybe something like the following using WeakReference : (I can't fully test right now or I'd just test it myself) 我在想使用WeakReference进行以下操作:(我现在无法完全测试,或者我自己进行测试)

List<WeakReference> weakReferences;
Thread myThread = new Thread(() => Foo());
WeakReference wr = new WeakReference(myThread);
weakReferences.Add(wr);  //adds a reference to the thread but still allows it to be garbage collected
myThread.Start();
myThread = null;  //get rid of reference so thread can be garbage collected

and then at the beginning of my onTimeElapsed event (run every 5 minutes): 然后在我的onTimeElapsed事件开始时(每5分钟运行一次):

foreach(WeakReference wr in weakReferences)
{
    Thread target = wr.Target as Thread;  //not sure if this cast is really possible
    if(target.IsAlive && otherLogic)
    {
         target.Abort();
    {
}

But I'm not sure exactly how WeakReference works. 但是我不确定WeakReference的工作原理。 Any ideas on how to properly do this? 有关如何正确执行此操作的任何想法?

Is myThread a method variable? myThread是方法变量吗? or...? 要么...?

In most scenarios, the thread will simply be garbage collected when possible. 在大多数情况下,只要可能, 就会简单地对线程进行垃圾回收。 There is no need to set myThread to null if myThread is a method variable, because that won't exist at the time. 如果myThread是方法变量,则无需将myThread设置为null ,因为那时该变量将不存在

I would, however, note that threads are actually pretty expensive objects (the stack alone is a pain to allocate). 但是,我要指出的是,线程实际上是非常昂贵的对象(仅堆栈是很难分配的)。 If possible, I would suggest either using the ThreadPool (if each item is short-lived), or a bespoke work queue (if longer), potentially with multiple workers servicing a single queue. 如果可能的话,我建议使用ThreadPool (如果每个项目都是短暂的)或定制的工作队列(如果更长),可能有多个工作人员在为一个队列服务。

As for terminating/aborting a thread... that is never a good idea; 至于终止/终止线程……这从来都不是一个好主意。 you have no idea what the thread is doing at that point. 不知道线程在那时在做什么。 After that, it is possible that your entire process is doomed. 之后,您的整个过程可能注定了失败。 If at all possible, consider having the worker check an "abort" flag occasionally. 如果可能话,请考虑工人偶尔检查“失败”标志。 If not possible, consider doing the work in a separate process . 如果不可能,请考虑在单独的过程中进行工作。 A process is even more expensive than a thread, but it has the advantage that it is isolated; 进程甚至比线程还昂贵,但是它具有隔离的优势。 you can kill it without impacting yourself. 您可以杀死它而不影响自己。 Of course, you could still corrupt any files it was working on, etc... 当然,您仍然可以破坏它正在处理的所有文件,等等。

Frankly, the main time I would ever consider aborting a thread is if my process is already dying, and I'm trying to put it out of misery ASAP. 坦白说,我主要考虑中止线程的主要时间是我的进程是否已经死了,而我正试图尽快摆脱它的痛苦。

Use thread pool. 使用线程池。 Don't spawn threads by yourself and don't invent the wheel. 不要自己产生线程,也不要发明轮子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM