希望专业人士帮忙翻译,万分感激!

[复制链接]
查看11 | 回复1 | 2011-3-9 14:38:14 | 显示全部楼层 |阅读模式
The problem with thread interrupts is that they are, by their very nature, intrusive. Using them will tend to make your program less well structured. A straightforward‐looking flow of control in one thread can suddenly be diverted because of an action initiated by another thread. This is another example of a facility that makes it harder to verify the correctness of a piece of program by local inspection. Unless alerts are used with great restraint, they will make your program unreadable, unmaintainable, and perhaps incorrect. I recommend that you very rarely use interrupts, and that the “Interrupt” method should be called only from the abstraction where the thread was created. For example, a package should not interrupt a caller’s thread that happens to be executing inside the package. This convention allows you to view an interrupt as an indication that the thread should terminate completely, but cleanly.
There are often better alternatives to using interrupts. If you know which object a thread is waiting on, you can more simply prod it by setting a Boolean flag and calling “Monitor.Pulse”. A package could provide additional entry points whose purpose is to prod a thread blocked inside the package on a long‐        erm wait. For example, instead of implementing “PipelinedRasterizer.Dispose” with the “Interrupt” mechanism we could have added a “Dispose” method to the “Queue” class, and called that.
Interrupts are most useful when you don’t know exactly what is going on. For example, the target thread might be blocked in any of several packages, or within a single package it might be waiting on any of several objects. In these cases an interrupt is certainly the best solution. Even when other alternatives are available, it might be best to use interrupts just because they are a single unified scheme for provoking thread termination.
Don’t confuse “Interrupt” with the quite distinct mechanism called “Abort”, which I’ll describe later. Only “Interrupt” lets you interrupt the thread at a well‐defined place, and it’s the only way the thread will have any hope of restoring the invariants on its shared variables.

回复

使用道具 举报

千问 | 2011-3-9 14:38:14 | 显示全部楼层
线程的问题是,他们,打断由于其本身的性质,强制的。利用他们就有可能让你的程序不那么的结构。一件简单的控制流看终于在一个线程可以突然转移的一种行为因为发起其他线程。这是另一个例子,一种设施,很难验证程序正确性的通过当地检验程序段。除非警报是用极大的克制,他们会让你的程序读取,不得不停下来了,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行