小馬文玩 發表於 2025-6-12 10:19:00

C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock

<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">引言:线程安全与锁的基本概念</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">线程安全</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">在多线程编程中,保障共享资源的安全访问依赖于有效的线程同步机制。理解并处理好以下两个核心概念至关重要:</p>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>线程安全:指某个类、方法或数据结构能够在被多个线程同时访问或修改时,依然保持内部状态的一致性,并产生预期的结果。这通常意味着需要对共享状态(如全局变量、静态变量或对象实例字段)的并发访问进行有效管控,防止数据损坏或不一致性。</li>
<li>竞态条件 (Race Condition): 是一种典型的并发缺陷。当多个线程在缺乏适当同步机制的情况下,无序地、竞争性地访问或修改共享资源时,程序执行结果变得依赖于无法预测的线程调度时序(即执行顺序)。这种不确定性常常会导致数据错误、程序崩溃或行为异常。竞态条件是线程安全缺失的直接体现。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">锁的基本概念</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>锁的本质:锁是一种同步工具,用于确保共享资源的互斥访问(一次只有一个线程使用)。当一个线程获得锁并执行被保护的代码段(临界区)时,其他试图获取同一锁的线程会被阻塞或等待,直到锁被释放。</li>
<li>锁的目标:在保证正确性的前提下,最大化并发度和系统吞吐量,最小化延迟。</li>
<li>锁的代价:
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0">
<li>阻塞开销:操作系统调度上下文切换的成本。</li>
<li>自旋开销:忙等待消耗CPU周期。</li>
<li>死锁风险:线程因相互等待对方释放锁而永久僵持。</li>
<li>优先级反转:低优先级线程持有高优先级线程需要的锁。</li>
<li>复杂性:使用不当可能导致程序难以理解和调试。</li>
<li>选择锁的依据:临界区大小、等待时间长短、竞争激烈程度、读/写比例、进程边界、公平性要求等。</li>
</ul>
</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">1. Monitor</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor</code>类提供了一种互斥锁机制,确保同一时间只有一个线程可以访问临界区。它是C#中<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">lock</code>语句的基础,通过<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor.Enter</code>和<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor.Exit</code>实现锁的获取和释放。</p>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">基于对象的内部 SyncBlock 索引关联的一个系统锁对象。每个.NET对象在堆上分配时,都有一个关联的 Sync Block Index (SBI)。当首次对这个对象使用 lock 时,SBI 被分配并指向操作系统内核中的一个真正的锁对象(比如 Windows 的 CRITICAL_SECTION)。</p>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">当锁已被占用时,后续请求的线程会进入内核等待状态,发生上下文切换。</p>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">Monitor.Wait(object obj), Monitor.Pulse(object obj), Monitor.PulseAll(object obj) 提供了在锁内等待特定条件成立的能力(类似 ConditionVariable),可用于构建生产者-消费者模式等。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">lock</code>语句是使用<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor</code>的简便方式:</p>
<pre class="language-csharp highlighter-hljs"><code>private&nbsp;readonly&nbsp;object&nbsp;_lock&nbsp;=&nbsp;new&nbsp;object();

lock&nbsp;(_lock)
{
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;临界区代码
}</code></pre>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">等价于:</p>
<pre class="language-x86asm highlighter-hljs"><code>Monitor.Enter(_lock);
try
{
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;临界区代码
}
finally
{
&nbsp;&nbsp;&nbsp;&nbsp;Monitor.Exit(_lock);
}</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>保护共享变量或非线程安全的集合</li>
<li>确保单一线程修改资源,如更新计数器或列表</li>
<li>需要简单互斥的临界区</li>
<li>临界区执行时间相对较长(大于上下文切换开销)</li>
<li>锁竞争不是极端激烈</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用私有对象(如<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">private readonly object _lock = new object();</code>)进行锁定,避免死锁。</li>
<li>保持临界区尽可能短,减少锁竞争。</li>
<li>避免锁定公共对象或类型(如<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">typeof(MyClass)</code>),因为其他代码可能也会锁定它们。</li>
<li>不要在锁内调用不可控的外部代码,可能导致死锁。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用简单,<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">lock</code>语句语法直观。</li>
<li>对于短临界区效率较高。</li>
<li>Monitor 锁是可重入(Reentrancy)的。同一个线程可以多次获得同一个锁对象上的锁(进入嵌套的 lock 块)。计数器会增加,只有等计数器归零时锁才会被释放。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>可能导致死锁,如果锁使用不当。Monitor.TryEnter(object obj, int timeoutMilliseconds) 允许设置等待超时,是避免死锁的重要手段。</li>
<li>不支持多读单写场景。</li>
<li>.NET 的 Monitor 锁是非公平的(Windows CLR 实现)。当锁释放时,操作系统从等待队列中选择下一个唤醒的线程是不确定的,不一定是最早等待的那个(这有助于提高吞吐量,但可能导致某些线程“饥饿”)。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">2. System.Threading.Lock</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">System.Threading.Lock</code>是.NET 9(C# 13)引入的新同步原语,旨在提供比<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor</code>更高效的互斥锁机制。它通过<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">EnterScope</code>方法支持<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">using</code>语句,确保锁自动释放,降低死锁风险。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">直接使用:</p>
<pre class="language-csharp highlighter-hljs"><code>private&nbsp;readonly&nbsp;Lock&nbsp;_lock&nbsp;=&nbsp;new&nbsp;Lock();

using&nbsp;(_lock.EnterScope())
{
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;临界区代码
}</code></pre>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">或在C# 13及以上版本中使用<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">lock</code>语句:</p>
<pre class="language-csharp highlighter-hljs"><code>lock&nbsp;(_lock)
{
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;临界区代码
}</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>与<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor</code>类似,用于保护共享资源。</li>
<li>适用于需要高性能的场景,如高并发系统。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用私有<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Lock</code>实例。</li>
<li>利用<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">using</code>语句确保锁自动释放。</li>
<li>避免将<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Lock</code>对象转换为<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">object</code>或其他类型,以防止编译器警告。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>性能比<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor</code>高约25%。</li>
</ul>
<pre class="language-ruby highlighter-hljs"><code>|&nbsp;Method&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Mean&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;Error&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;StdDev&nbsp;&nbsp;&nbsp;|&nbsp;Ratio&nbsp;|&nbsp;Gen0&nbsp;&nbsp;&nbsp;|&nbsp;Allocated&nbsp;|&nbsp;Alloc&nbsp;Ratio&nbsp;|
|-------------------------&nbsp;|----------:|---------:|---------:|------:|-------:|----------:|------------:|
|&nbsp;CountTo1000WithLock&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;107.22&nbsp;us&nbsp;|&nbsp;1.561&nbsp;us&nbsp;|&nbsp;1.460&nbsp;us&nbsp;|&nbsp;&nbsp;1.00&nbsp;|&nbsp;0.1221&nbsp;|&nbsp;&nbsp;&nbsp;1.06&nbsp;KB&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1.00&nbsp;|
|&nbsp;CountTo1000WithLockClass&nbsp;|&nbsp;&nbsp;75.73&nbsp;us&nbsp;|&nbsp;0.884&nbsp;us&nbsp;|&nbsp;0.827&nbsp;us&nbsp;|&nbsp;&nbsp;0.71&nbsp;|&nbsp;0.1221&nbsp;|&nbsp;&nbsp;&nbsp;1.05&nbsp;KB&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0.99&nbsp;|</code></pre>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Dispose</code>模式自动释放锁,降低死锁风险。</li>
<li>与<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">lock</code>语句无缝集成,语法简洁。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>需要.NET 9或更高版本。</li>
<li>开发者对其熟悉度较低。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">3. Mutex</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li><code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Mutex</code>(互斥锁)是一种支持进程间同步的互斥锁机制,确保只有一个线程或进程访问共享资源。</li>
<li>可以通过命名互斥锁实现跨进程同步。</li>
<li>比 Monitor/lock 重得多(涉及系统调用)。</li>
<li>支持安全访问系统资源(如文件、硬件设备句柄)。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<pre class="language-java highlighter-hljs"><code>private&nbsp;static&nbsp;Mutex&nbsp;_mutex&nbsp;=&nbsp;new&nbsp;Mutex();

_mutex.WaitOne();
//&nbsp;临界区代码
_mutex.ReleaseMutex();</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>跨进程同步,如确保应用程序的单一实例运行。</li>
<li>保护共享资源,如文件或数据库。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用命名互斥锁(如<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">new Mutex(false, "MyAppMutex")</code>)进行进程间同步。</li>
<li>尽快释放互斥锁,减少阻塞时间。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">注意</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>重入性:命名 Mutex 默认是可重入的(同一个线程)。匿名(未命名)Mutex 在 .NET Framework 默认可重入,在 .NET Core+ 中默认为 .NoRecursion 行为。</li>
<li>自动释放:如果持有 Mutex 的线程终止(例如崩溃),操作系统会自动释放锁(这可能导致程序逻辑错误),并且下一个等待的线程可能接收到 AbandonedMutexException。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>支持进程间同步。</li>
<li>提供可靠的互斥访问。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>由于涉及内核模式转换,性能较低。</li>
<li>开销较大,不适合高频短临界区。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">4. SpinLock</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">SpinLock</code>是一种互斥锁,线程在尝试获取锁时会通过自旋(循环检查)等待锁可用,适用于极短的临界区。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<pre class="language-csharp highlighter-hljs"><code>private&nbsp;SpinLock&nbsp;_spinLock&nbsp;=&nbsp;new&nbsp;SpinLock();

bool&nbsp;lockTaken&nbsp;=&nbsp;false;
try
{
&nbsp;&nbsp;&nbsp;&nbsp;_spinLock.Enter(ref&nbsp;lockTaken);
&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;临界区代码
}
finally
{
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(lockTaken)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_spinLock.Exit();
&nbsp;&nbsp;&nbsp;&nbsp;}
}</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>极短的临界区,锁持有时间短于上下文切换成本。</li>
<li>高并发场景,锁竞争频繁但持续时间短。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>仅用于极短临界区。</li>
<li>避免在低竞争或长临界区场景中使用。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>对于短临界区开销低。</li>
<li>无上下文切换。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>如果锁持有时间长,会浪费CPU周期。</li>
<li>不适合长临界区。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">5. ReaderWriterLockSlim</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">ReaderWriterLockSlim</code>允许多个线程同时读取资源,但写操作互斥,且写时不允许读操作,适合读多写少的场景。</p>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">有几种不同的锁定模式:</p>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>读取锁 (Read Lock):共享模式,允许多个线程同时持有。</li>
<li>写入锁 (Write Lock):独占模式,一旦持有,排斥所有读取锁和其他写入锁。</li>
<li>可升级读取锁 (Upgradeable Read Lock):一种特殊模式,允许一个读取线程在持有读锁的同时,后续有需要时可以原子性地升级 (Upgrade)为写入锁(避免先释放读锁再尝试拿写锁过程中出现竞态或死锁)</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<pre class="language-csharp highlighter-hljs"><code>private&nbsp;readonly&nbsp;ReaderWriterLockSlim&nbsp;_rwLock&nbsp;=&nbsp;new&nbsp;ReaderWriterLockSlim();

public&nbsp;string&nbsp;ReadData()
{
&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.EnterReadLock();&nbsp;//&nbsp;获取读锁
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;安全读取共享数据
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;_cachedData;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.ExitReadLock();&nbsp;//&nbsp;释放读锁
&nbsp;&nbsp;&nbsp;&nbsp;}
}

public&nbsp;void&nbsp;UpdateData(string&nbsp;newData)
{
&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.EnterWriteLock();&nbsp;//&nbsp;获取写锁
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;安全更新共享数据
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_cachedData&nbsp;=&nbsp;newData;
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.ExitWriteLock();&nbsp;//&nbsp;释放写锁
&nbsp;&nbsp;&nbsp;&nbsp;}
}

//&nbsp;使用可升级锁&nbsp;(避免“写者饥饿”风险):
public&nbsp;void&nbsp;UpdateIfCondition(string&nbsp;newData,&nbsp;Func&lt;bool&gt;&nbsp;condition)
{
&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.EnterUpgradeableReadLock();&nbsp;//&nbsp;获取可升级读锁
&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(condition())
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.EnterWriteLock();&nbsp;//&nbsp;升级为写锁
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;安全更新共享数据
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_cachedData&nbsp;=&nbsp;newData;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.ExitWriteLock();&nbsp;//&nbsp;降级回可升级读锁
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;finally
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_rwLock.ExitUpgradeableReadLock();&nbsp;//&nbsp;释放锁
&nbsp;&nbsp;&nbsp;&nbsp;}
}</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>读操作频繁、写操作较少的场景,如缓存系统。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>确保写操作快速,减少读线程阻塞。</li>
<li>避免长时间持有写锁,防止写者饥饿。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">注意</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>ReaderWriterLockSlim 性能更好,语义更清晰,设计更合理。强烈建议总是使用 ReaderWriterLockSlim 而不是 ReaderWriterLock。</li>
<li>性能特征:在纯读场景下并发度接近无锁;写操作开销比普通互斥锁略高(需要管理读写状态转换);升级操作开销适中。</li>
<li>公平性与策略:提供了构造参数 LockRecursionPolicy.NoRecursion / .SupportsRecursion 和 ReaderWriterLockSlim(lockRecursionPolicy) 来控制递归行为。也涉及公平性问题(如读者优先或写者优先,ReaderWriterLockSlim 有机制防止写者饿死)。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>允许多个线程同时读取,提高性能。</li>
<li>适合读多写少场景。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用复杂,需管理读写锁状态。不恰当地嵌套获取不同类型的锁(特别是尝试升级锁失败时等待其他锁)会导致死锁。</li>
<li>可能导致写者饥饿。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">6. Semaphore 和 SemaphoreSlim</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li><code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Semaphore</code>控制对资源池的并发访问,限制同时访问的线程数。</li>
<li><code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Semaphore</code>:内核模式,支持跨进程、命名。</li>
<li>SemaphoreSlim:轻量级用户模式实现(必要时退化到内核),仅进程内有效,性能开销远小于 Semaphore。<strong style="color: rgba(53, 148, 247, 1); font-weight: bold; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); width: auto; height: auto; border-radius: 0; border: 3px none rgba(0, 0, 0, 0.4); padding: 0; margin: 0">绝大多数进程内场景应优先使用 SemaphoreSlim。</strong></li>
<li>SemaphoreSlim 默认使用公平队列(FIFO),有助于防止饥饿。Semaphore 的公平性由操作系统决定。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<pre class="language-java highlighter-hljs"><code>private&nbsp;Semaphore&nbsp;_semaphore&nbsp;=&nbsp;new&nbsp;Semaphore(3,&nbsp;3);&nbsp;//&nbsp;初始和最大计数

//WaitOne/WaitAsync:尝试获取一个令牌(信号)。若无可用令牌则阻塞/异步等待
_semaphore.WaitOne();
//&nbsp;Release:释放一个令牌
_semaphore.Release();</code></pre>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">SemaphoreSlim</code>使用方式类似。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>限制并发访问特定资源的数量(API调用限流、连接池控制、异步任务并发度控制)。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Semaphore</code>进行进程间同步,<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">SemaphoreSlim</code>用于进程内。</li>
<li>设置合理的初始和最大计数。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>灵活控制并发级别。</li>
<li><code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">SemaphoreSlim</code>性能较高。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用较复杂。</li>
<li>可能导致死锁。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">7. EventWaitHandle、AutoResetEvent、ManualResetEvent、ManualResetEventSlim</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">事件用于线程间信号传递。<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">AutoResetEvent</code>在信号一个等待线程后自动重置;<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">ManualResetEvent</code>保持信号状态直到手动重置;<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">ManualResetEventSlim</code>是轻量级版本。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">AutoResetEvent</code>示例:</p>
<pre class="language-java highlighter-hljs"><code>private&nbsp;AutoResetEvent&nbsp;_event&nbsp;=&nbsp;new&nbsp;AutoResetEvent(false);

_event.WaitOne();&nbsp;//&nbsp;等待信号
//&nbsp;执行操作

_event.Set();&nbsp;//&nbsp;发送信号</code></pre>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">ManualResetEvent</code>示例:</p>
<pre class="language-java highlighter-hljs"><code>private&nbsp;ManualResetEvent&nbsp;_event&nbsp;=&nbsp;new&nbsp;ManualResetEvent(false);

_event.WaitOne();&nbsp;//&nbsp;等待信号
//&nbsp;执行操作

_event.Set();&nbsp;//&nbsp;发送信号
_event.Reset();&nbsp;//&nbsp;重置事件</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>生产者-消费者模式。</li>
<li>等待特定任务完成。</li>
<li>启动/停止信号广播、一次性初始化完成指示。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>使用<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">AutoResetEvent</code>进行一对一信号传递。</li>
<li>使用<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">ManualResetEvent</code>广播信号给多个线程。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>提供简单的信号传递机制。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>状态管理复杂,尤其是<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">ManualResetEvent</code>。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">8. CountdownEvent</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">初始化一个计数(N)。线程调用 Signal() 来递减计数。当计数达到0时,所有在该对象上 Wait() 的线程被释放。适用于“N个任务完成后继续”的场景。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<pre class="language-java highlighter-hljs"><code>private&nbsp;CountdownEvent&nbsp;_countdown&nbsp;=&nbsp;new&nbsp;CountdownEvent(3);

_countdown.Wait();&nbsp;//&nbsp;等待计数归零
//&nbsp;执行操作

_countdown.Signal();&nbsp;//&nbsp;减少计数</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>主线程等待一组分散操作的完成,模拟部分 Task.WaitAll 效果但有更多控制(可在操作执行过程中动态调整计数)。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>设置正确的初始计数。</li>
<li>确保所有信号都发送,避免死锁。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>便于等待多个事件。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>仅限于计数场景。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">9. Barrier</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">允许多个线程分阶段执行任务,并确保所有参与线程在一个共同的屏障点(Phase)同步汇合(都到达后)才能继续下一阶段。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<pre class="language-java highlighter-hljs"><code>private&nbsp;Barrier&nbsp;_barrier&nbsp;=&nbsp;new&nbsp;Barrier(3);

_barrier.SignalAndWait();&nbsp;//&nbsp;信号并等待其他线程
//&nbsp;继续执行</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>并行算法中协调多个线程的阶段,如分治算法、复杂数据并行流水线处理。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>确保所有参与者调用<code style="background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">SignalAndWait</code>。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>协调多线程分阶段执行。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>设置复杂,需确保所有线程参与。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">10. SpinWait</span></h2>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">原理</span></h4>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器"><code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">SpinWait</code>通过自旋等待条件成立,适合短时间等待。</p>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">操作方式</span></h4>
<pre class="language-javascript highlighter-hljs"><code>SpinWait.SpinUntil(()&nbsp;=&gt;&nbsp;someCondition);</code></pre>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">应用场景</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>短时间等待条件成立,如检查标志位。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">最佳实践</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>用于预期很快满足的条件。</li>
<li>避免长时间自旋。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">优点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>避免上下文切换。</li>
</ul>
<h4 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: block; flex-direction: unset; float: unset; height: auto; justify-content: unset; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 18px; color: rgba(37, 181, 86, 0.77); line-height: 1.5em; letter-spacing: 0; align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: inline-block; font-weight: bold; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; border-top: 1px none rgba(0, 0, 0, 1); border-right: 1px none rgba(0, 0, 0, 1); border-bottom: 2px solid rgba(37, 181, 86, 0.77); border-left: 1px none rgba(0, 0, 0, 1); padding: 2px 10px; margin: 0">缺点</span></h4>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>长时间等待浪费CPU资源。</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">11. 无锁替代</span></h2>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0" data-tool="mdnice编辑器">
<li>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0">不可变性 (Immutability):一旦创建对象就不可修改。避免了修改引起的同步需求(readonly 字段,记录类型 record)。</p>
</li>
<li>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0">线程本地存储 (Thread-Local Storage - TLS):ThreadStaticAttribute, AsyncLocal 变量,ThreadLocal。每个线程使用自己独立的数据副本(适用性有限)。</p>
</li>
<li>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0">Interlocked 类:提供对简单类型(int, long, IntPtr, float, double, object 引用)执行原子操作的静态方法(Increment, Decrement, Add, Exchange, CompareExchange)。是最轻量级的“锁”,基于 CPU 的原子指令实现,性能极高,无锁开销。</p>
<pre class="language-csharp highlighter-hljs"><code>private&nbsp;int&nbsp;_counter&nbsp;=&nbsp;0;
public&nbsp;void&nbsp;IncrementSafely()
{
&nbsp;&nbsp;&nbsp;&nbsp;Interlocked.Increment(ref&nbsp;_counter);&nbsp;//&nbsp;原子+1
}
public&nbsp;void&nbsp;SetIfEqual(int&nbsp;newValue,&nbsp;int&nbsp;expected)
{
&nbsp;&nbsp;&nbsp;&nbsp;Interlocked.CompareExchange(ref&nbsp;_counter,&nbsp;newValue,&nbsp;expected);&nbsp;//&nbsp;CAS
}</code></pre>
</li>
<li>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0">基于任务的异步模式 (TAP) 与 Task:</p>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0">
<li>Channel (System.Threading.Channels):.NET Core 2.1+ 引入。高性能、无锁/有界可选的生产者-消费者队列替代方案(取代 BlockingCollection 和无锁队列手动实现)。支持单/多生产者、单/多消费者。是编写异步管道、处理背压 (Backpressure) 的首选。</li>
</ul>
<pre class="language-csharp highlighter-hljs"><code>var&nbsp;channel&nbsp;=&nbsp;Channel.CreateUnbounded&lt;T&gt;();
//&nbsp;生产者
await&nbsp;channel.Writer.WriteAsync(item);
//&nbsp;消费者
while&nbsp;(await&nbsp;channel.Reader.WaitToReadAsync())
&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(channel.Reader.TryRead(out&nbsp;var&nbsp;item))&nbsp;{&nbsp;...&nbsp;}</code></pre>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0">
<li>ValueTask / IValueTaskSource:Task 的轻量级替代(减少了堆分配),尤其在同步完成路径上优化显著。</li>
</ul>
</li>
<li>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0">Immutable Collections (System.Collections.Immutable):提供线程安全的不可变集合,通过原子替换整个集合引用来“修改”数据。读操作非常高效(无需锁),写操作创建新集合,适合读远多于写的共享数据。</p>
</li>
<li>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0">专为并发访问设计的内置集合:</p>
<ul style="list-style-type: disc; color: rgba(0, 0, 0, 1); padding: 0 0 0 25px; margin: 8px 0">
<li>ConcurrentDictionary&lt;TKey, TValue&gt;:高效、低锁竞争、可并行的字典。</li>
<li>ConcurrentQueue / ConcurrentStack:先进先出(FIFO) / 后进先出(LIFO)队列,基于CAS实现,避免锁争用。</li>
<li>BlockingCollection:有界/无界生产者-消费者队列(底层使用 ConcurrentQueue 等),提供 Take() 阻塞语义(Channel 通常是更好的异步选择)。支持优雅取消和完成通知。</li>
</ul>
</li>
</ul>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">12. 结语</span></h2>
<p style="color: rgba(43, 43, 43, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; text-indent: 0; padding: 8px 0; margin: 0" data-tool="mdnice编辑器">选择合适的同步原语取决于应用程序需求,如是否需要进程间同步、读写分离或高性能。<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">System.Threading.Lock</code>是C# 13 中的新选择,性能优于<code style="color: rgba(53, 148, 247, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0; background: none left top / auto no-repeat scroll padding-box border-box rgba(27, 31, 35, 0.05); width: auto; height: auto; border-radius: 4px; overflow-wrap: break-word; font-family: &quot;Operator Mono&quot;, Consolas, Monaco, Menlo, monospace; word-break: break-all; border-top: 3px none rgba(0, 0, 0, 1); border-right: 3px none rgba(0, 0, 0, 0.4); border-bottom: 3px none rgba(0, 0, 0, 0.4); border-left: 3px none rgba(0, 0, 0, 0.4); padding: 2px 4px; margin: 0 2px">Monitor</code>,适合大多数互斥场景。开发者应根据场景权衡性能、复杂性和功能,确保线程安全的同时避免死锁和性能瓶颈。</p>
<h2 style="align-items: unset; background: none left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; display: flex; flex-direction: unset; float: unset; height: auto; justify-content: center; line-height: 1.5em; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0; margin: 30px 0 15px; border: 1px none rgba(0, 0, 0, 1)" data-tool="mdnice编辑器"><span class="content" style="font-size: 22px; line-height: 1.8em; letter-spacing: 0; align-items: unset; background: linear-gradient(0deg, rgba(231, 247, 252, 1) 40%, rgba(0, 0, 0, 0) 40%) left top / auto no-repeat scroll padding-box border-box rgba(0, 0, 0, 0); border-radius: 0; box-shadow: none; color: rgba(112, 188, 234, 1); display: block; font-weight: bold; font-style: normal; flex-direction: unset; float: unset; height: auto; justify-content: unset; overflow-x: unset; overflow-y: unset; position: relative; text-align: left; text-indent: 0; text-shadow: none; transform: none; width: auto; -webkit-box-reflect: unset; padding: 0 10px 0 0; margin: 0; border: 1px none rgba(0, 0, 0, 1)">13. 附件表格对比</span></h2>
<table style="display: table; text-align: left">
<thead>
<tr><th style="color: rgba(89, 89, 89, 1); font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; font-weight: bold; width: auto; height: auto; border-radius: 0; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>同步原语</div>
</th><th style="color: rgba(89, 89, 89, 1); font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; font-weight: bold; width: auto; height: auto; border-radius: 0; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>互斥性</div>
</th><th style="color: rgba(89, 89, 89, 1); font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; font-weight: bold; width: auto; height: auto; border-radius: 0; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>允许多读</div>
</th><th style="color: rgba(89, 89, 89, 1); font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; font-weight: bold; width: auto; height: auto; border-radius: 0; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>进程间支持</div>
</th><th style="color: rgba(89, 89, 89, 1); font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; font-weight: bold; width: auto; height: auto; border-radius: 0; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>性能</div>
</th><th style="color: rgba(89, 89, 89, 1); font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; font-weight: bold; width: auto; height: auto; border-radius: 0; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>示例用例</div>
</th><th style="color: rgba(89, 89, 89, 1); font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; font-weight: bold; width: auto; height: auto; border-radius: 0; min-width: 85px; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是否支持可重入</div>
</th></tr>
</thead>
<tbody style="font-size: 14px; line-height: 1.5em; letter-spacing: 0.02em; text-align: left; font-weight: normal; border-image: initial; border: 0">
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>Monitor</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>高</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>保护共享变量</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>System.Threading.Lock</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>极高</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>高性能互斥锁</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>Mutex</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>低</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>进程间同步</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>SpinLock</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>极高</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>极短临界区</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>ReaderWriterLockSlim</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是(写)</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>中</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>读多写少资源</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>Semaphore</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>中</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>限制并发访问</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>SemaphoreSlim</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>高</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>进程内并发控制</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>EventWaitHandle</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>是</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>中</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>线程/进程间信号传递</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>ManualResetEventSlim</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>高</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>进程内信号传递</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>CountdownEvent</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>中</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>等待多个信号</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>Barrier</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>中</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>分阶段线程执行</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(248, 248, 248, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>Interlocked</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>极高</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>原子操作</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
<tr style="color: rgba(89, 89, 89, 1); background: none left top / auto no-repeat scroll padding-box border-box rgba(255, 255, 255, 1); width: auto; height: auto">
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>SpinWait</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>无</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>高</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>短时间自旋等待</div>
</td>
<td style="min-width: 85px; border-radius: 0; border: 1px solid rgba(204, 204, 204, 0.4); padding: 5px 10px; text-align: left">
<div>否</div>
</td>
</tr>
</tbody>
</table>
<blockquote class="custom-blockquote multiquote-1" style="border-radius: 8px; background: none left top / auto no-repeat scroll padding-box border-box rgba(64, 184, 250, 0.1); width: auto; height: auto; box-shadow: 0 0 rgba(0, 0, 0, 0); display: block; overflow-x: auto; overflow-y: auto; border: 1px solid rgba(64, 184, 255, 0.4); padding: 10px 10px 10px 20px; margin: 20px 0" data-tool="mdnice编辑器"><span style="display: block; color: rgba(64, 184, 250, 0.5); font-size: 28px; line-height: 1.5em; letter-spacing: 0; text-align: left; font-weight: bold">❝</span>
<p style="text-indent: 0; color: rgba(59, 59, 59, 1); font-size: 14px; line-height: 1.8em; letter-spacing: 0.02em; text-align: left; font-weight: normal; padding: 8px 0; margin: 0"><strong><span style="color: rgba(255, 0, 0, 1)">由于资料验证范围太广,难免会有遗漏,如果上述表格内的内容有问题,请在评论区告诉我</span></strong></p>
</blockquote>

</div>
<div id="MySignature" role="contentinfo">
    <p>
    本文来自博客园,作者:AI·NET极客圈,转载请注明原文链接:https://www.cnblogs.com/code-daily/p/18924622
</p>
<img src="https://images.cnblogs.com/cnblogs_com/blogs/272929/galleries/2447197/o_250311050355_AI.NET%20Logo.png" width="61.8%" style="margin-left:20px"/>

<p>
    欢迎关注我们的公众号,作为.NET工程师,我们聚焦人工智能技术,探讨 AI 的前沿应用与发展趋势,为你立体呈现人工智能的无限可能,让我们共同携手共同进步。
</p><br><br>
来源:https://www.cnblogs.com/code-daily/p/18924622
頁: [1]
查看完整版本: C# 锁机制全景与高效实践:从 Monitor 到 .NET 9 全新 Lock