Java中的并发Map数据结构

时间:2016-11-11 23:02:09

标签: java performance data-structures concurrency atomic

我有以下数据结构。我正在寻找一个数据结构,其行为类似于以索引为键的并发哈希映射。它需要是线程安全的。我希望获得比具有以下数据结构的并发哈希映射更好的性能,我的实验告诉我,我做了。

所以我的问题是: 我错过了这个实现的东西吗? 我知道bitset不是原子的,但是add / remove中的二进制信号应该处理它。

public class NewDS
{
    private BitSet bs = null;
    private AtomicIntegerArray arr = null;
    private int size;
    private Semaphore semaphore = new Semaphore(1, true);
    public NewDS( int size) {
            this.bs = new BitSet(size);
            this.arr = new AtomicIntegerArray(size);
    }
    public int put (int hashChunkId) throws InterruptedException {
            semaphore.acquire();
            int index = bs.nextClearBit(0);
            arr.set(index, hashChunkId);
            semaphore.release();
            return index;
    }

    public void remove(int index) throws InterruptedException {
            semaphore.acquire();
            bs.clear(index);
            semaphore.release();
    }

}

0 个答案:

没有答案
相关问题