All Downloads are FREE. Search and download functionalities are using the official Maven repository.

shz.core.queue.l.ConcurrentDLinkedQueue Maven / Gradle / Ivy

There is a newer version: 10.3.1
Show newest version
package shz.core.queue.l;

import shz.core.constant.ArrayConstant;
import shz.core.node.DSNode;

/**
 * 元素类型为double基于链表的队列
 * 

* 20+16*(n+1)(n为元素个数) *

* B=8+16*(n+3) */ public class ConcurrentDLinkedQueue extends ConcurrentLinkedQueue { protected ConcurrentDLinkedQueue() { } public static ConcurrentDLinkedQueue of() { return new ConcurrentDLinkedQueue(); } @Override protected final Double get(DSNode node) { return node.val; } public final void offer(double e) { writeLock.lock(); try { DSNode temp = tail; tail = DSNode.of(e); if (head == null) head = tail; else temp.next(tail); ++size; } finally { writeLock.unlock(); } } public final double poll() { writeLock.lock(); try { double val = head(); head = head.next(); if (head == null) tail = null; --size; return val; } finally { writeLock.unlock(); } } public final double head() { readLock.lock(); try { return head.val; } finally { readLock.unlock(); } } public final double tail() { readLock.lock(); try { return tail.val; } finally { readLock.unlock(); } } public final double[] toArray() { if (size == 0) return ArrayConstant.EMPTY_DOUBLE_ARRAY; double[] array = new double[size]; int idx = 0; DSNode current = head; while (current != null) { array[idx++] = current.val; current = current.next(); } return array; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy