静态对象上获取锁应该阻止其他请求的线程? [英] acquiring a lock on a static object should block on other request threads?

查看:141
本文介绍了静态对象上获取锁应该阻止其他请求的线程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,看完后的this,我有点困惑锁定机制或只是从根本上混淆了请求。

So after reading this, I'm a bit confused about the "lock" mechanism or just fundamentally confused about requests.

是一给​​定类的静态变量访问(假设一台服务器)的所有要求?据我了解,他们不是。

Are static variables of a given class accessible to all requests ( assuming one server )? From what I understand, they're not.

和假设静态/ const的变量不是多个请求访问下,当我们锁定(someLockObject)它不应该阻止其他请求,因为锁获得与该对象关联的独占锁。再次,该对象(即someLockObject)是我的假设下,所有的请求不同。

And under the assumption that static/const variables are not accessible to multiple requests, when we lock(someLockObject) it should not block other requests because "lock" obtains the exclusive lock associated with that object. And again, that object (ie. someLockObject) is different for all requests under my assumption.

<一个href=\"http://stackoverflow.com/questions/2997438/will-lock-statement-block-all-threads-in-the-proccess-appdomain\">This答案,还有其他几个人,意味着我错了的东西。如果锁定仅几个街区的关键部分,这意味着执行相同件code中的锁定code块,那么所有的线程,为什么我们要在对象上阻止呢?我可能不会作出太大的意义我自己。

This answer, as well as a few others, implies that I'm wrong about something. And if "lock" only blocks that critical section, meaning that all threads that execute the same piece of code in the "lock" code-block, then why do we have to block on an object at all? I might not be making much sense myself.

推荐答案

您产生混淆从原始的假设:

Your confusion arises from your original assumption:

是一给​​定类的静态变量访问(假设一台服务器)的所有要求?据我了解,他们不是。

Are static variables of a given class accessible to all requests ( assuming one server )? From what I understand, they're not.

静态字段是整个在整个应用领域的每一个线程每个实例相同。出于实用的目的,在一个服务器实例,那基本上意味着静态字段是在所有请求共享。

Static fields for a particular class are the same across for every instance in every thread in the entire application domain. For practical purposes, on one server instance, that basically means a static field is shared across all requests.

恒值更是全球性:指的是常量值将实际产生编译code字面常量。例如:

Constant values are even more global: referring to a const value will actually produce a literal constant in compiled code. For example:

Console.WriteLine(3);

...产生完全相同的code为:

... produces exactly the same code as:

Console.WriteLine(Numbers.Three);

...给出一类是这样的:

... given a class like this:

public class Numbers { public const int Three = 3; }

这篇关于静态对象上获取锁应该阻止其他请求的线程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆