实现一个BlockQueue

实现一个不阻塞的Queue

public class QueueUseLink<E> {
    private Node<E> head;
    private Node<E> tail;
    int capacity;
    int size;

    public QueueUseLink(int capacity) {
        this.capacity = capacity;
    }

    static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value) {
            this.value = value;
        }
    }

    boolean offer(E e) {
        final Node<E> newNode = new Node<>(e);
        if (size == capacity) {
            return false;
        }

        size++;
        if (tail == null) {
            head = tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }
        return true;
    }

    E peek() {
        return head.value;
    }

    E poll() {
        if (head == null) {
            return null;
        } else {
            E val = head.value;
            head = head.next;
            return val;
        }
    }


    public static void main(String[] args) {
        final QueueUseLink<String> queue = new QueueUseLink<>(3);
        queue.offer("a");
        queue.offer("b");
        queue.offer("c");
        final boolean b = queue.offer("d");

        final String peek1 = queue.peek();
        final String peek2 = queue.peek();
        final String poll1 = queue.poll();
        final String poll2 = queue.poll();
        final String poll3 = queue.poll();
        System.out.println();
    }
}

使用wait、notify实现BlockQueue

public class BlockQueueUseWaitNotify<E> {

    private Node<E> head;
    private Node<E> tail;
    int capacity;
    int size;
    private Object lock = new Object();

    public BlockQueueUseWaitNotify(int capacity) {
        this.capacity = capacity;
    }

    static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value) {
            this.value = value;
        }
    }

    /**
     * 仅实现阻塞的put跟take
     * 非阻塞的方法已经在QueueUseLink实现
     */
    public void put(E e) throws InterruptedException {
        synchronized (lock) {
            Node<E> newNode = new Node<>(e);

            for (;;) {
                if (capacity == size) {
                    lock.wait();
                    continue;
                }

                if (tail == null) {
                    head = tail = newNode;
                } else {
                    tail.next = newNode;
                    tail = newNode;
                }
                size++;
                break;
            }

        }
    }

    public E take() throws InterruptedException {
        synchronized (lock) {
            for (;;) {
                if (head == null) {
                    lock.wait();
                } else {
                    E val = head.value;
                    head = head.next;
                    size--;
                    lock.notify();
                    return val;
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        BlockQueueUseWaitNotify<String> blockQueue = new BlockQueueUseWaitNotify<>(3);

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(10);
                // 10秒后取出一个元素
                final String take4 = blockQueue.take();
                System.out.println("take4=" + take4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        blockQueue.put("a");
        blockQueue.put("b");
        blockQueue.put("c");
        blockQueue.put("d");

        final String take1 = blockQueue.take();
        final String take2 = blockQueue.take();
        final String take3 = blockQueue.take();
        System.out.println();

    }
}

使用Condition的signal、await实现BlockQueue

public class BlockQueueUseCondition<E> extends AbstractQueuedSynchronizer {
    private Node<E> head;
    private Node<E> tail;
    int capacity;
    int size;

    static class Node<E> {
        E value;
        Node<E> next;

        public Node(E value) {
            this.value = value;
        }
    }

    ReentrantLock lock = new ReentrantLock();
    Condition putCondition = lock.newCondition();
    Condition takeCondition = lock.newCondition();

    public BlockQueueUseCondition(int capacity) {
        this.capacity = capacity;
    }

    public void put(E e) throws InterruptedException {
        Node<E> newNode = new Node<>(e);

        lock.lock();

        for (;;) {
            if (capacity == size) {
                putCondition.await();
                continue;
            }

            if (tail == null) {
                head = tail = newNode;
            } else {
                tail.next = newNode;
                tail = newNode;
            }
            size++;
            break;
        }
        takeCondition.signal();

        lock.unlock();
    }

    public E take() throws InterruptedException {
        lock.lock();

        for (;;) {
            if (head == null) {
                takeCondition.await();
            } else {
                E val = head.value;
                head = head.next;
                size--;
                putCondition.signal();
                lock.unlock();
                return val;
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        BlockQueueUseCondition<String> blockQueue = new BlockQueueUseCondition<>(3);

        new Thread(() -> {
            try {
                // 阻塞等待获取
                final String take = blockQueue.take();
                System.out.println("take0:" + take);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();



        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(5);
                // 5秒后取出一个元素
                final String take4 = blockQueue.take();
                System.out.println("take4=" + take4);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        blockQueue.put("a");
        blockQueue.put("b");
        blockQueue.put("c");
        blockQueue.put("d");
        blockQueue.put("e");

        final String take1 = blockQueue.take();
        final String take2 = blockQueue.take();
        final String take3 = blockQueue.take();
        System.out.println();
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值