根据线程安全的相关知识,分析以下代码,当调用test方法时i>10时是否会引起死锁?并简要说明理由。
public void test(int i) { lock(this) { if (i>10) { i--; test(i); } } }
答:不会发生死锁,(但有一点int是按值传递的,所以每次改变的都只是一个副本,因此不会出现死锁。但如果把int换做一个object,那么死锁会发生)
我的问题:这里说传入引用类型参数会死锁,我试过几种做法都不能产生死锁
把int换成object,也不会死锁,因为这里从头到尾只有当前线程在调用,lock语句执行多少次都无所谓的。
大侠 能否给我个例子 能产生死锁的例子,就以这个例子为基础弄一个,非常感谢
class Test { private object a_lock = new object(); private object b_lock = new object(); public void M1() { Console.WriteLine("thread {0} is trying to acquire a_lock in M1", Thread.CurrentThread.ManagedThreadId); lock (a_lock) { Thread.Sleep(2000); Console.WriteLine("thread {0} got a_lock, trying to acquire b_lock in M1", Thread.CurrentThread.ManagedThreadId); lock (b_lock) { Console.WriteLine("thread {0} got both a_lock and b_lock in M1", Thread.CurrentThread.ManagedThreadId); } } } public void M2() { Console.WriteLine("thread {0} is trying to acquire b_lock in M2", Thread.CurrentThread.ManagedThreadId); lock (b_lock) { Thread.Sleep(2000); Console.WriteLine("thread {0} got b_lock, trying to acquire a_lock in M2", Thread.CurrentThread.ManagedThreadId); lock (a_lock) { Console.WriteLine("thread {0} got both a_lock and b_lock in M2", Thread.CurrentThread.ManagedThreadId); } } } } class Program { static void Main(string[] args) { var printer = new Test(); Thread t1 = new Thread(() => printer.M1()); Thread t2 = new Thread(() => printer.M2()); t1.Start(); t2.Start(); } }
@何海:
class Program { static void Main(string[] args) { TestClass tc = new TestClass(); System.Threading.Thread t = new System.Threading.Thread(tc.test); t.Start(1000); System.Threading.Thread t2 = new System.Threading.Thread(tc.test); t2.Start(2000); } } public class TestClass { public void test(object n) { int i = int.Parse(n.ToString()); lock (this) { if (i > 10) { i--; test(i); } } } }
楼上说的很对,但是换成多个线程也不会产生死锁,
因为有i--,
每次i 的传递并非传递的是副本,确实是i已经递减的结果了,