Lock
Lock
s are built in Java; every Object
has a lock that a thread may acquire when it enters a synchronized
block. We discussed that already. In some programming code, there are situations when this kind of structure is not optimal.
In some situations, the structure of locks may be lined up to avoid deadlock. It may be needed to acquire lock A before B and to acquire B before C. However, A should be released as soon as possible, not to prevent access to resource protected by lock D, but also needing lock A before it. In complex and highly parallel structures, the locks are structured many times into trees where accessing a resource a thread should climb down along the tree to a leaf representing the resource. In this climbing, the thread gets hold of a lock on a node, then a lock on a node below it, and then releases the lock above, just like a real climber descending (or climbing up if you imagine the tree with the leafs at the top, which is more realistic, nevertheless graphs usually...