在.NET线程中止和中断的区别 [英] Difference between Abort and Interrupt in Threads in .NET

查看:113
本文介绍了在.NET线程中止和中断的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么Thraed.Abort()和了Thread.interrupt()之间的差异。我怎么能叫他们在一个线程安全的Manner.It将是有益的,如果提供简单的例子。

What is the difference between Thraed.Abort() and Thread.Interrupt(). How can I call them in a Thread Safe Manner.It would be helpful,if simple example is provided.

推荐答案

首先,这些都不是好线程同步构造。

First of all, neither of these are good thread synchronization constructs.

首先,的 Thread.Abort的说:我不在乎你在做什么,只是停止这样做,并保留一切,因为它是现在。它基本上是说:嘿,打败它的方式编程。如果你的线程是有打开的文件,这些文件将保持打开状态,直到垃圾收集得到周围,以确定您的对象。

First, Thread.Abort says "I don't care what you're doing, just stop doing it, and leave everything as it is right now". It's basically the programming way of saying "Hey, beat it". If your thread is having files open, those files will be left open until garbage collection gets around to finalizing your objects.

Thread.Abort的应该只被使用,甚至那么很可能不是在该情况下,当该线程内运行的应用程序域被推倒,优选只有当该过程被终止。

Thread.Abort should only ever be used, and even then probably not, in the case when the app domain that the thread is running inside is being torn down, preferably only when the process is being terminated.

其次,<一HREF =htt​​p://msdn.microsoft.com/en-us/library/system.threading.thread.interrupt%28VS.71%29.aspx>了Thread.interrupt 是一个相当奇怪的野兽。它基本上说:我不在乎你在等待什么,停下等着呢。这里奇怪的是,如果线程当前未在等待什么,那不是我不在乎你要等待下一次什么,但是当你做,立即停止等着呢。

Secondly, Thread.Interrupt is a rather strange beast. It basically says "I don't care what you're waiting for, stop waiting for it". The strange thing here is that if the thread isn't currently waiting for anything, it's instead "I don't care what you're going to wait for next, but when you do, stop waiting for it immediately".

这两个都是你的,这不是设计被告知这样的事情一个线程强加你的意志的迹象。

Both of these are signs that you're imposing your will on a thread that wasn't designed to be told such things.

要正确中止一个线程,该线程应定期检查某种标志,无论是简单的挥发性布尔变量,或一个事件对象。如果标志说:你现在应该终止后,线程应该通过有序的方式返回自行终止。

To abort a thread properly, the thread should periodically check some kind of flag, be it a simple volatile Boolean variable, or an event object. If the flag says "You should now terminate", the thread should terminate itself by returning from the methods in an orderly fashion.

要正确地唤醒一个线程,线程应该,在它必须等待同步对象的地方,包括请停止​​等待的对象,它也将等待。所以基本上它会为它要么需要变得标志着对象,或请停止等待的对象变成信号,确定哪一个做的,做正确的事。

To properly wake a thread, a thread should, in places where it has to wait for synchronization objects, include a "please stop waiting" object that it also waits on. So basically it would for either the object it needs becomes signaled, or the "please stop waiting" object becomes signaled, determine which one that did, and do the right thing.

,你应该使用正常的同步对象,如事件,互斥,信号等写你的线程。

So instead of Thread.Abort and Thread.Interrupt, you should write your threads using normal synchronization objects, like events, mutexes, semaphores, etc.

出于同样的原因, Thread.Suspend 并的 Thread.Resume 应单独,并且它们也被在后废弃.NET版本。

For the same reason, Thread.Suspend and Thread.Resume should be left alone, and they have also been obsoleted in the later versions of .NET.

这篇关于在.NET线程中止和中断的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆