美文网首页
无锁数组队列

无锁数组队列

作者: 万福来 | 来源:发表于2020-06-02 19:20 被阅读0次

    无锁数组队列

    static class FreeLockArrayQueue<E> {
            private AtomicReferenceArray<E> queue;
            private AtomicInteger putIndex;
            private AtomicInteger takeIndex;
            private int size;
    
            FreeLockArrayQueue(int size) {
                this.size = size;
                queue = new AtomicReferenceArray<E>(size);
                putIndex = new AtomicInteger(0);
                takeIndex = new AtomicInteger(0);
            }
    
            public void put(E e) {
                for (; ; ) {
                    int curr = putIndex.get();
                    if (queue.compareAndSet(curr % size, null, e)) {
                        putIndex.incrementAndGet();
                        break;
                    }
                    if (putIndex.get() < takeIndex.get() + size) {
                        putIndex.incrementAndGet();
                    }else{
                        LockSupport.parkNanos(this, 3000L);
                    }
                }
    
            }
    
            public E take() {
                for (; ; ) {
                    int curr = takeIndex.get();
                    if (curr <= putIndex.get()) {
                        E expect = queue.get(curr % size);
                        if (expect != null && queue.compareAndSet(curr % size, expect, null)) {
                            takeIndex.incrementAndGet();
                            return expect;
                        }
                    }
                    Thread.currentThread().yield();
                    if (takeIndex.get() < putIndex.get()) {
                        takeIndex.incrementAndGet();
                    }else{
                        LockSupport.parkNanos(this, 3000L);
                    }
                }
            }
    
        }
    

    相关文章

      网友评论

          本文标题:无锁数组队列

          本文链接:https://www.haomeiwen.com/subject/owspzhtx.html