O
oliver789
Hello,
I'm working on a tree where the node classes store their child nodes
in a TreeMap. Now I don't want to lock the entire tree when elements
are added or removed. So I'm thinking of the node class to have it's
own ReentrantReadWriteLock. When I traverse down the tree I close each
node's own read write lock when looking for the appropriate child node
to pick to continue the traversal and open the lock once I have picked
the right child node and continue the traversal to the next deeper
level in the same way. Because there is a context switch possible
while jumping to the next lower node (previous lock was opened, new
lock of lower level not yet acquired), the class that holds the root
node has an AtomicInteger that counts the visitors of the tree. If
visitorCount >= 1 no thread must add or remove any nodes in the tree.
So far I see no logical error in that approach. My concern is that
this approach might not be very efficient under heavy load (large
number of concurrent accesses of the tree), because for every visited
node in the tree the node's lock has to be opened and closed. That is
quite a bit of locking that is going on there. Does any body have a
better idea or knows some articles that describe better approaches?
Thanks, Oliver Holp
I'm working on a tree where the node classes store their child nodes
in a TreeMap. Now I don't want to lock the entire tree when elements
are added or removed. So I'm thinking of the node class to have it's
own ReentrantReadWriteLock. When I traverse down the tree I close each
node's own read write lock when looking for the appropriate child node
to pick to continue the traversal and open the lock once I have picked
the right child node and continue the traversal to the next deeper
level in the same way. Because there is a context switch possible
while jumping to the next lower node (previous lock was opened, new
lock of lower level not yet acquired), the class that holds the root
node has an AtomicInteger that counts the visitors of the tree. If
visitorCount >= 1 no thread must add or remove any nodes in the tree.
So far I see no logical error in that approach. My concern is that
this approach might not be very efficient under heavy load (large
number of concurrent accesses of the tree), because for every visited
node in the tree the node's lock has to be opened and closed. That is
quite a bit of locking that is going on there. Does any body have a
better idea or knows some articles that describe better approaches?
Thanks, Oliver Holp