In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章将为大家详细讲解有关C#中如何解决同线程Lock语句递归不会死锁的问题,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
前几天在网上闲逛,无意中看到有这么一道题及其答案,如下:
根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。
public void test(int i) { lock(this) { if (i > 10) { i--; test(i); } } }
答:不会发生死锁,(但有一点int是按值传递的,所以每次改变的都只是一个副本,因此不会出现死锁。但如果把int换做一个object,那么死锁会发生)
当我看到这道题时,我心里只有两个答案,1、会发生死锁,2、不会。^_^说了当没说。我觉得会发生死锁的理由是:同一线程只能进入lock语句一次,如果这个线程没有退出lock语句就不能再次进入lock语句。而不会发生死锁的理由是,同一线程可以多次进入到lock语句中。
我将这段代码拷入VS中运行,发现没有进入死锁,于是想找个权威的理由来解释它,终于在《CLR via C#》第二版(中文版,清华大学出版社出版)的第530页中第7行找到了这样的描述:"同样需要引起注意的是线程可以递归拥有同步块"。即同一线程可以递归调用lock语句。
以上只讨论了单线程的情况,下面的代码给出的两个线程的情况:
using System;using System.Threading;namespace LockDemo{ class Program { static void Main(string[] args) { Program p = new Program(); MyObj obj = new MyObj(); //第一个线程 Thread thread1 = new Thread(p.test); thread1.Name = "thread1"; //第一个线程 Thread thread2 = new Thread(p.test); thread2.Name = "thread2"; //启动线程 thread1.Start(obj); thread2.Start(obj); Console.Read(); } public void test(object obj) { lock (this) { if (((MyObj)obj).value > 10) { ((MyObj)obj).value--; Console.Write(Thread.CurrentThread.Name + ":"); Console.WriteLine(((MyObj)obj).value); Thread.Sleep(10); test(obj); } else { Console.WriteLine(Thread.CurrentThread.Name); } } } } /// /// 将一个值类型封装在一个类中,以便多个线程调用方便 /// public class MyObj { public int value; public MyObj() { //将初始值赋为20 value = 20; } }}
下面是运行结果:
由于thread1先进入lock语句,所以锁一直由thread1占有,递归调用直到不满足条件为止,thread1释放锁后,thread2进入lock语句时,发现当前已经不满足递归条件了,即:i < 10了,所以直接退出。
让我觉得奇怪的是网上给出的答案,即括号中的文字说明,明明代码中是对this对象加的锁,与传递的参数何关?找个int是按值传递的理由解释不会发生死锁让我觉得很奇怪。
关于C#中如何解决同线程Lock语句递归不会死锁的问题就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.