温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

LinkedBlockingQueue原理是什么

发布时间:2021-06-22 15:43:31 来源:亿速云 阅读:166 作者:chen 栏目:大数据

本篇内容主要讲解“LinkedBlockingQueue原理是什么”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“LinkedBlockingQueue原理是什么”吧!

LinkedBlockingQueue

构成链表的节点表示
static class Node < E > {
    E item;
    Node < E > next;
    Node(E x) {
        item = x;
    }
}


链表属性
private final int capacity;

private final AtomicInteger count = new AtomicInteger();

transient Node < E > head;

private transient Node < E > last;
private final ReentrantLock takeLock = new ReentrantLock();
private final ReentrantLock putLock = new ReentrantLock();
private final Condition notEmpty = takeLock.newCondition();

private final Condition notFull = putLock.newCondition();
使用的方法有
private void signalNotEmpty();
private void signalNotFull();
private void enqueue(Node < E > node);
private E dequeue();
双锁

// 把固定的加锁顺序封装在方法内,确保所有的对两把锁加锁的顺序都是一致的。
void fullyLock() {
    putLock.lock();
    takeLock.lock();
}

// 把固定的释放锁顺序封装在方法内,确保所有的对两把锁的释放顺序都是一致的。
void fullyUnlock() {
    takeLock.unlock();
    putLock.unlock();
}


构造方法
public LinkedBlockingQueue(int capacity) {
    if (capacity <= 0) throw new IllegalArgumentException();
    this.capacity = capacity;
    last = head = new Node < E > (null);
}
public LinkedBlockingQueue(Collection < ? extends E > c)

public int size()
public int remainingCapacity()

public void put(E e) throws InterruptedException
public boolean offer(E e, long timeout, TimeUnit unit)

public boolean offer(E e)

public E take() throws InterruptedException
public E poll(long timeout, TimeUnit unit) throws InterruptedException
public E poll()
/**从头拿**/
public E peek()


public boolean remove(Object o)
public boolean contains(Object o)
public Object[] toArray()
public < T > T[] toArray(T[] a)
    ......

到此,相信大家对“LinkedBlockingQueue原理是什么”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI