Java - 两个线程进入相同的条件并通过

问题描述 投票:2回答:3

我正在研究BTree,我想同步它,但问题是:

我不想同步整个方法,因为它具有复杂的功能。

我的问题是:如何防止前两个或三个线程传递root==null的第一个条件?

public void add(TrackingDevice device) {
    // Tree is Empty, then add a new elemet to root
    if (root == null ) {
        root = new BTreeNode(true);
        root.keys[0] = device;
        root.n++;
    } else {
        /*
        * The root is Full
        */
        if (root.n == 2 * t - 1) {
            splitRoot();
            root.insert(device);
            height++;
        } else {
            root.insert(device);
        }
    }
}
java multithreading synchronization
3个回答
1
投票

我会使用AtomicReference及其compareAndSet方法。

然后,多个线程可以传递条件root.get()== null并创建根节点。但只有最快的线程才会通过compareAndSet写入值。


0
投票

我只是建议:实现一个类似信号量的结构。定义一个同步计数器,你的线程每次进入时都会递减(注意:这个操作必须是互斥的)并且如果counter == 0,则阻止Threads进入一个section。


0
投票

您可以使用AtomicReference<BTreeNode>作为对根节点的引用,并使用compareAndSet(null, new BTreeNode(true))来防止进入if块。如果经常调用add方法,这种方法最终会创建许多短暂的BTreeNode实例。如果这是一个问题,你可以先“手动”检查AtomicReference是否包含null,只有这样才能调用compareAndSet

但是,我会完全摆脱null检查,并最初设置一个n = 0的根节点。这使得代码更加统一,因为不需要检查根是否存在。

© www.soinside.com 2019 - 2024. All rights reserved.