在讲CancellationTokenSource之前我决定先讲一下lock和Interlocked,如果能很好的理解这两个,再去理解CancellationTokenSource就会方便很多,由于我也是后起使用多线程,使用的时候就是直接运用FramWork4的东西,这样导致了很多东西学起来很吃力,当回顾了以前的知识点后,发现新出的东西如此好理解。
先看一下Lock的使用,下面是一个例子。
<span >namespace<span > LockConsole{
<span >class<span > Program
{
<span >static <span >object lockint = <span >0<span >;
<span >static <span >voID<span > Main()
{
Task[] tList=<span >new Task[<span >10<span >];
Thread[] threads </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Thread[<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">10</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">]; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>; i < <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">10</span>; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">var</span> action = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Action<<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span>><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">(Withdraw); Task t </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Task(action,i); t<a href="https://m.jb51.cc/tag/List/" target="_blank" >List</a>[i] </span>=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> t; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Thread t = new Thread(new ThreadStart(acc.DoTransactions)); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">threads[i] = t;</span>
<span > }
<span >for (<span >int i = <span >0; i < <span >10; i++<span >)
{
tList[i].Start();
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">threads[i].Start();</span>
<span > }
Console.Read();
}
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span> Withdraw(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">object</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> i) { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> amount = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">.Parse(i.ToString()); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> balance = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1000</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">lock</span> (lockint)<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"><a href="https://www.jb51.cc/tag/shanchu/" target="_blank" >删除</a>lock就会造成 线程混乱</span>
<span > {
<span >if (balance >=<span > amount)
{
Console.Writeline(<span >"<span >当前线程{0},当前状态{1}<span >",Thread.CurrentThread.GetHashCode(),Thread.CurrentThread.ThreadState + <span >"<span >Balance before Withdrawal : <span >" +<span > balance);
Console.Writeline(<span >"<span >当前线程{0},Thread.CurrentThread.ThreadState + <span >"<span >Amount to Withdraw : -<span >" +<span > amount);
balance = balance -<span > amount;
Console.Writeline(<span >"<span >当前线程{0},Thread.CurrentThread.ThreadState + <span >"<span >Balance after Withdrawal : <span >" +<span > balance);
} } }}
}
输出结果如下,他的线程都是一个接一个的,线程11后是线程12,然后是13,14,11等。每个线程都是等到执行完了下一个才执行。
在看一下没有Lock的结果,如下图,线程是混乱的,12线程的函数没执行完13就开始了。
有了上面的例子,Lock就很好理解了,他是为了保障资源同一时间只被一个线程使用,虽然该例子中没有使用Lock的资源,但线程还是一个接一个的执行,因为使用了lock线程就会一个接一个执行。
接下来看一下interLock,这是一个MSDN的例子,我觉得不错,他的意思是,把usingResource作为一个锁,当多线程开始运行,函数UseResource()需要判断usingResource的值,当usingResource等于0的时候,当前线程不运行,否则运行,当本线程运行时,要修改usingResource的值为1,这样确保其他线程不运行,即同一时间只运行一个线程。
如果这样需求用到开发中,会出现一个问题,那就是当一个线程改变usingResource的值的一瞬间,别的线程读取了usingResource的值,那这个线程也被运行了。
这时就需要InterLock了。代码如下。
<span >namespace<span > MyInterlockedExchangeExampleClass{
<span >class<span > MyInterlockedExchangeExampleClass
{
<span >//<span >0 for false,1 for true.
<span >private <span >static <span >int usingResource = <span >0<span >;
</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">const</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> numThreadI<a href="https://www.jb51.cc/tag/tera/" target="_blank" >tera</a>tions = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">5</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">const</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> numThreads = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">10</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Main() { Thread myThread; Random rnd </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> Random(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>; i < numThreads; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { myThread </span>= <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span> Thread(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">new</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> ThreadStart(MyThreadProc)); myThread.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a> </span>= String.Format(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">Thread{0}</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span>,i + <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Wait a random amount of time before starting next thread.</span> Thread.Sleep(rnd.Next(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1000</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)); myThread.Start(); } } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">private</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">vo<a href="https://m.jb51.cc/tag/ID/" target="_blank" >ID</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> MyThreadProc() { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">for</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">int</span> i = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span>; i < numThreadI<a href="https://www.jb51.cc/tag/tera/" target="_blank" >tera</a>tions; i++<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">) { UseResource(); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Wait 1 second before next attempt.</span> Thread.Sleep(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1000</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); } } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">A simple method that den<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>s reentrancy.</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">static</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">bo<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> UseResource() { </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">0 indicates that the method is not in use. </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">原始值是0,判断是时候<a href="https://m.jb51.cc/tag/shiyong/" target="_blank" >使用</a>原始值,但判断后值为1,<a href="https://m.jb51.cc/tag/jinxing/" target="_blank" >进行</a>了<a href="https://m.jb51.cc/tag/shezhi/" target="_blank" >设置</a></span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">if</span> (<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span> == Interlocked.Exchange(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">ref</span> usingResource,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">1</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">)) { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">{0} acquired the lock</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,Thread.CurrentThread.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Code to access a resource that is not thread safe wo<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>d go here. </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Sim<a href="https://m.jb51.cc/tag/ul/" target="_blank" >ul</a>ate some work</span> Thread.Sleep(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">500</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">{0} exiting lock</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,Thread.CurrentThread.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">//</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;">Release the lock</span> Interlocked.Exchange(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">ref</span> usingResource,<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800080;">0</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">true</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">else</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"> { Cons<a href="https://m.jb51.cc/tag/ol/" target="_blank" >ol</a>e.Write<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>ne(</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"> {0} was den<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>d the lock</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;">"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">,Thread.CurrentThread.<a href="https://m.jb51.cc/tag/name/" target="_blank" >name</a>); </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">return</span> <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;">false</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;">; } }}
}
理解了lock和interlock后下一章讲解CancellationTokenSource。
总结以上是内存溢出为你收集整理的C#线程安全使用(三)全部内容,希望文章能够帮你解决C#线程安全使用(三)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)