为了账号安全,请及时绑定邮箱和手机立即绑定

C# 关于lock关键字的疑惑...高手请进...

C# 关于lock关键字的疑惑...高手请进...

扬帆大鱼 2018-07-26 12:05:17
string a="abc";lock(a){....};在实际使用中我发现锁定的不是a对象,而是以a对象作为标识的{}内的代码块.. 但发现资料上是像图中那样说的,是我没理解正确呢还是资料错误?请高手解惑.using System;using System.Threading;class Program {      static void Main(string[] args)     {         Student s = new Student { Name="yh"};         Thread t=new Thread (new ThreadStart(()=>{lock (s) { Thread.Sleep(100000); }}));         t.Start();         Thread.Sleep(1000);         Console.WriteLine(s.Name);//这里没有等锁的线程结束才执行..所以我觉得资料说的不对..你们认为呢?                  Console.ReadKey();     }   }internal class Student {    public string Name { get; set; }
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 768 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信