我不知道我是否理解你的问题,正如你所说当你锁定一个子树进行写入时,整个结构都被锁定了。
因此,简单的解决方案是为整个结构设置一个RW锁。
顺便说一句,java.util.concurrent.atomic不会帮助你更多的RW锁定树。
如果您希望能够独立锁定兄弟姐妹,您可以使用第二种解决方案(每个节点都有一个对其父节点的引用的锁定树)。
锁定节点将使用其写锁定锁定它并使用读锁定锁定每个父节点。
在子节点不能锁定父节点时,因为锁定已经获取读锁定的子节点时无法获取其写锁定。
仅当没有其他线程写入锁定任何父级时,才允许锁定子级。
上述锁是独占锁。
(读写锁的另一个名称是共享独占锁)
要添加共享锁,每个节点还需要一个原子整数,指示:
如果是积极的,间接写锁子元素的数量;如果它是负数,则节点被读锁定的次数。
此外,将读取锁定节点及其父节点以避免在父节点上获取新的写锁定。
伪代码:
Node {
// fields
parent: Node
lock: RWLock
count: AtomicInteger
}
public boolean trylocktree(node: Node, exclusive: boolean) {
if (exclusive) {
return trylocktree_ex(node, true);
} else {
return trylocktree_sh(node);
}
}
private boolean switch_count(i: AtomicInteger, diff: int) {
// adds diff to i if the sign of i is the same as the sign of diff
while (true) {
int v = i.get();
if (diff > 0 ? v < 0 : v > 0)
return false;
if (i.compareAndSet(v, v + diff))
return true;
}
}
private boolean trylocktree_ex(node: Node, writing: boolean) {
// check if a node is read-locked
if (!switch_count(node.count, 1))
return false;
// lock using the lock type passed as an arg
if (!node.lock(writing).trylock()) {
node.count--;
return false;
}
// read-lock every parent
if (!trylocktree_ex(node.parent, false)) {
node.count--
node.lock(writing).unlock();
return false;
}
return true;
}
private boolean trylocktree_sh(node: Node) {
// mark as shared-locked subtree
if (!switch_count(node.count, -1))
return false;
// get shared-lock on parents
if (!readlock_recursively(node)) {
node.count++;
return false;
}
return true;
}
private boolean readlock_recursively(node: Node) {
if (!node.lock(false).trylock())
return false;
if (!readlock_recursively(node.parent)) {
node.lock(false).unlock();
return false;
}
return true;
}如果无法获取任何锁定,则解锁锁定的内容并稍后重试(可以使用全局条件变量,超时等来实现此目的)。
编辑:添加代码以读取锁定/写入锁定树