基於LinkedBlockingQueue源碼自我實現安全執行緒隊列

來源:互聯網
上載者:User

標籤:style   blog   color   io   java   ar   for   div   art   

LinkedBlockingQueue是一個阻塞的、安全執行緒的、由鏈表實現的雙向隊列,和ArrayBlockingQueue一樣,是最普通也是最常用的阻塞隊列。現基於LinkedBlockingQueue源碼自我實現一個單向的、簡化版的LinkedBlockingQueue.

package com.lzq.newInterview;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;public class MyLinkedBlockingQueue<E> {    private Node<E> head;// 隊頭    private Node<E> tail;// 隊尾    private static final int DEFALT_SIZE = 20;    private int size;// 隊列最大長度    private int count;// 隊列當前容量    private ReentrantLock lock = new ReentrantLock();    private Condition empty = lock.newCondition();    private Condition full = lock.newCondition();    public MyLinkedBlockingQueue() {        head = null;        tail = null;        size = DEFALT_SIZE;    }    public MyLinkedBlockingQueue(int size) {        head = null;        tail = null;        this.size = size;    }    // 隊列尾部插入元素    public void offer(E e) {        if (e == null)            return;        try {            lock.lock();            while (count == size) {                full.await();            }            if (head == null && tail == null) {                Node<E> node = new Node<E>(e, null);                head = node;                tail = node;            } else {                Node<E> node = new Node<E>(e, null);                tail.next = node;                tail = node;            }            count++;            System.out.println("offer: count=" + this.count);            empty.signal();        } catch (Exception exp) {            exp.printStackTrace();        } finally {            lock.unlock();        }    }    public E poll() {        try {            lock.lock();            while (count == 0) {                empty.await();            }            E oldValue = head.value;            head = head.next;            if (head == null)                tail = null;            count--;            System.out.println("poll: count=" + this.count);            full.signal();            return oldValue;        } catch (Exception e) {            e.printStackTrace();        } finally {            lock.unlock();        }        return null;    }    static class Node<E> {        Node<E> next;        E value;        Node(E value, Node<E> next) {            this.value = value;            this.next = next;        }    }    public static void main(String[] args) {        final MyLinkedBlockingQueue<Integer> queue = new MyLinkedBlockingQueue<Integer>();        for (int i = 0; i < 100; i++) {            final int num = i;            Thread t = new Thread(new Runnable() {                public void run() {                    queue.offer(num);                }            });            t.start();        }        for (int i = 0; i < 100; i++) {            Thread t = new Thread(new Runnable() {                public void run() {                    queue.poll();                }            });            t.start();        }    }}

部分運行結果:

offer: count=1
offer: count=2
offer: count=3
offer: count=4
offer: count=5
offer: count=6
offer: count=7
offer: count=8
offer: count=9
offer: count=10
offer: count=11
offer: count=12
poll: count=11
poll: count=10
poll: count=9
poll: count=8
poll: count=7
poll: count=6
poll: count=5
poll: count=4
poll: count=3
poll: count=2
poll: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
offer: count=2
offer: count=3
poll: count=2
poll: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
offer: count=2
poll: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
offer: count=2
poll: count=1
poll: count=0
offer: count=1
offer: count=2
poll: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
offer: count=2
poll: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
poll: count=0
offer: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
offer: count=2
poll: count=1
poll: count=0

基於LinkedBlockingQueue源碼自我實現安全執行緒隊列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.