Today, I see an article that describes in detail the concept of lock and lock granularity, and why should I lock it as follows? Locking is designed to prevent the confusion of different threads from accessing the same shared resource.
A metaphor: People are different threads, toilets are shared resources
When you are in the bathroom, you must lock the door, this is the lock, as long as you are inside, the bathroom is locked, only after you come out to be used by others. Imagine what it would look like if the bathroom door was unlocked.
What is the size of the lock? The so-called lock granularity is how much you want to lock the range.
For example, you are in the bathroom at home, you just lock the bathroom can be, do not need to lock the whole house to let the family door, the bathroom is your lock granularity.
How to calculate the reasonable lock granularity?
In fact, the bathroom is not just for the toilet, but also can bathe, wash hands. This involves the optimization of the lock granularity problem.
You bathe in the bathroom, in fact, others can also go inside to wash their hands, as long as the isolation can be, if the toilet, bathtub, wash table are separated relatively independent, in fact, the bathroom can be used at the same time for three people,
Of course, three people do not do the same thing. This will refine the lock granularity, you can just close the bathroom door when you are in the shower, others may still go in to wash their hands. If the toilet was designed without dividing different functional areas
Can not achieve the maximum use of toilet resources. This is the importance of the design architecture. Thank you blog http://blog.chinaunix.net/uid-20758579-id-1876916.html
Detailed analogy of lock and lock granularity