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.
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.
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:
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
staticfield is shared across all requests.Constant values are even more global: referring to a
constvalue will actually produce a literal constant in compiled code. For example:… produces exactly the same code as:
… given a class like this: