线程并发处理之lock学习

    在应用程序中使用多个线程的一个好处是每个线程都可以异步执行。然而,线程的异步特性意味着必须协调对资源(如文件句柄、网络连接和内存)的访问。否则,两个或更多的线程可能在同一时间访问相同的资源,而每个线程都不知道其他线程的操作。结果将产生不可预知的数据损坏。这个时候我们就需要lock上场了。

Lock的作用

Lock获取给定对象的互斥锁,保证相应的代码块运行时,不会被其他线程中断;直到该对象被释放时其他线程才能访问相应的代码块;

Lock实现本质

通过一下的代码和MSIL,我们可以看到lock的实现是通过在try中调用System.Threading.Monitorenterfinally中调用其exit方法实现的

代码实例如下

public void PrintByInnerObj(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

object obj=newobject();

lock(obj)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"-- has unlock");

}

以上代码对应的MSIL代码如下

Lock的锁定范围

Lock的参数必须为引用类型的对象,该对象代表了锁定的范围,对象不同锁定的范围也不同。

 

锁定参数为待锁定代码块内声明的对象,锁定范围为该代码块

public void PrintByInnerObj(objectgreating)

锁定参数为待锁定代码块所在类的私有字段,锁定范围为该类具体的一个实例

锁定参数为待锁定代码块所在类的私有静态字段,锁定范围为该类所有的实例

锁定参数为某一字符串,锁定范围为与该字符串值相等的所有字符串

锁定参数为this,锁定范围为所有能访问到this的地方

锁定参数为某个类的System.Type的实例,锁定范围为所有的地方

锁定public的实例字段,锁定范围同锁定this

锁定参数为public的静态字段,锁定范围与锁定system.type相同

 

public void PrintLockByPublicStaticObj(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock (publicStaticObj)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"--has unlock");

}

public void PrintLockByPublicInstanceObj(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock (publicInstanceObj)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"--has unlock");

}

public void PrintLockByClass(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock (typeof(MyLockTest))

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

Thread.Sleep(1000);

}

Console.WriteLine(greating+"-- has unlock");

}

 

 

public void PrintLockByThis(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock (this)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"--has unlock");

}

 

 

public void PrintLockByStringObj(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock (stringObj)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"--has unlock");

}

 

public void PrintLockByString(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock ("lock")

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"--has unlock");

}

 

 

public void PrintLockByStaticObj(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock (staticObj)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

Thread.Sleep(10000);

}

Console.WriteLine(greating+"-- has unlock");

}

 

 

public void PrintByInstanceObj(objectgreating)

{

Console.WriteLine(greating+"-- before lock");

lock (instanceObj)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"-- has unlock");

}

 

 

{

Console.WriteLine(greating+"-- before lock");

object obj=newobject();

lock(obj)

{

Console.WriteLine(greating+"-- is locking");

Console.WriteLine(greating.ToString());

System.Threading.Thread.Sleep(10000);

}

Console.WriteLine(greating+"-- has unlock");

}

 

 

.method public hidebysig instance void PrintByInnerObj(object greating) cil managed
{
// Code size 116 (0x74)
.maxstack 2
.locals init ([0] object obj,
[1] bool '<>s__LockTaken0',
[2] object CS$2$0000,
[3] bool CS$4$0001)
IL_0000: nop
IL_0001: ldarg.1
IL_0002: ldstr "-- before lock"
IL_0007: call string [mscorlib]System.String::Concat(object,
object)
IL_000c: call void [mscorlib]System.Console::WriteLine(string)
IL_0011: nop
IL_0012: newobj instance void [mscorlib]System.Object::.ctor()
IL_0017: stloc.0
IL_0018: ldc.i4.0
IL_0019: stloc.1
.try
{
IL_001a: ldloc.0
IL_001b: dup
IL_001c: stloc.2
IL_001d: ldloca.s '<>s__LockTaken0'
IL_001f: call void [mscorlib]System.Threading.Monitor::Enter(object,
bool&)
IL_0024: nop
IL_0025: nop
IL_0026: ldarg.1
IL_0027: ldstr "-- is locking"
IL_002c: call string [mscorlib]System.String::Concat(object,
object)
IL_0031: call void [mscorlib]System.Console::WriteLine(string)
IL_0036: nop
IL_0037: ldarg.1
IL_0038: callvirt instance string [mscorlib]System.Object::ToString()
IL_003d: call void [mscorlib]System.Console::WriteLine(string)
IL_0042: nop
IL_0043: ldc.i4 0x2710
IL_0048: call void [mscorlib]System.Threading.Thread::Sleep(int32)
IL_004d: nop
IL_004e: nop
IL_004f: leave.s IL_0061
} // end .try
finally
{
IL_0051: ldloc.1
IL_0052: ldc.i4.0
IL_0053: ceq
IL_0055: stloc.3
IL_0056: ldloc.3
IL_0057: brtrue.s IL_0060
IL_0059: ldloc.2
IL_005a: call void [mscorlib]System.Threading.Monitor::Exit(object)
IL_005f: nop
IL_0060: endfinally
} // end handler
IL_0061: nop
IL_0062: ldarg.1
IL_0063: ldstr "-- has unlock"
IL_0068: call string [mscorlib]System.String::Concat(object,
object)
IL_006d: call void [mscorlib]System.Console::WriteLine(string)
IL_0072: nop
IL_0073: ret
} // end of method MyLockTest::PrintByInnerObj

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值