Will lock() statement block all threads in the process/appdomain?

后端 未结 6 1231
执念已碎
执念已碎 2020-12-17 17:47

Maybe the question sounds silly, but I don\'t understand \'something about threads and locking and I would like to get a confirmation (here\'s why I ask).

So, if I h

6条回答
  •  醉梦人生
    2020-12-17 18:33

    lock will block all threads in that application from accessing the myLockHolder object.

    So if you have 10 instances of the application running you'll get 10 requests to the server while the object is locked on each. The moment you exit the lock statement, the next request will process in that application, but as long as Cache[key] is not null, it won't access the database..

    The number of actual requests you get depends on what happens here:

      if (Cache[key] == null)
      {
         Cache[key] = LengthyDatabaseCall();
      }
    

    If LengthyDatabaseCall(); fails, the next request will try and access the database server and retrieve the information as well, so really your best case scenario is that there will only be 10 requests to the server.

提交回复
热议问题