基于AQS+双向链表实现队列先进先出

学习AQS写的一个模拟案例

package com.tom.xiangyun.ch04_aqs;

import com.tom.tuling.UnsafeFactory;
import sun.misc.Unsafe;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 使用双向链表实现队列
 *
 * @author 钟棋炜
 * @date 2023-08-22 20:52
 */
public class QueueDoubleLinked {

    private transient volatile Node head;
    private transient volatile Node tail;
    private static final Unsafe unsafe = UnsafeFactory.getUnsafe();

    private static final long headOffset;
    private static final long tailOffset;
    private static final long nextOffset;


    private final ReentrantLock takeLock = new ReentrantLock();

    private final AtomicInteger count = new AtomicInteger();


    static {
        try {
            assert unsafe != null;
            headOffset = unsafe.objectFieldOffset(QueueDoubleLinked.class.getDeclaredField("head"));
            tailOffset = unsafe.objectFieldOffset(QueueDoubleLinked.class.getDeclaredField("tail"));
            nextOffset = unsafe.objectFieldOffset(Node.class.getDeclaredField("next"));

        } catch (Exception ex) {
            throw new Error(ex);
        }
    }

    public QueueDoubleLinked() {
    }

    private  boolean compareAndSetHead(Node update) {
        return unsafe.compareAndSwapObject(this, headOffset, null, update);
    }

    private boolean compareAndSetTail(Node expect, Node update) {
        return unsafe.compareAndSwapObject(this, tailOffset, expect, update);
    }

    //入队
    public String add(String element) {
        Node node = new Node(element);
        enq(node);
        return element;
    }

    //出队 先进
    public Object poll() {
        final AtomicInteger count = this.count;
        if (count.get() == 0) {
            return null;
        }

        Object x = null;
        final ReentrantLock takeLock = this.takeLock;
        takeLock.lock();

        try {
            if (count.get() > 0) {
                x = dequeue();
                count.getAndDecrement();
            }
        } finally {
            takeLock.unlock();
        }
        return x;
    }

    private Object dequeue() {
        Node h = head;
        Node first = head.next;
        h.next = h;
        head = first;
        Object obj = first.data;
        first.data = null;
        return obj;
    }


    private Node enq(final Node node) {//尾插法
        final AtomicInteger count = this.count;

        for (; ; ) {
            Node t = tail;
            if (t == null) {
                if (compareAndSetHead(new Node())) {
                    tail = head;
                }
            } else {
                node.prev = t;
                if (compareAndSetTail(t, node)) {
                    t.next = node;
                    count.getAndIncrement();
                    return t;
                }
            }
        }
    }

    static final class Node {

        Node prev;//前置节点
        Node next;//后置节点

        Object data; //存放数据

        public Node() {

        }

        public Node(Object data) {
            this.data = data;
        }

    }

    public static void main(String[] args) {
        QueueDoubleLinked queue = new QueueDoubleLinked();
        queue.add("tom");
        queue.add("no");
        queue.add("xx");
        queue.add("zz");

        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue);
    }
}

你可能感兴趣的:(数据结构和算法,链表,java,数据结构)