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: 2024.0.2
Show newest version
package shz.core.queue.l;

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

import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.DoubleConsumer;

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

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

* B=8+16*(n+3) */ public class ConcurrentDLinkedQueue extends ConcurrentLinkedQueue { private static final long serialVersionUID = -2580079814940380222L; public static ConcurrentDLinkedQueue of() { return new ConcurrentDLinkedQueue(); } 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 { checkEmpty(); double val = head.val; head = head.next(); if (head == null) tail = null; --size; return val; } finally { writeLock.unlock(); } } public final double head() { readLock.lock(); try { checkEmpty(); return head.val; } finally { readLock.unlock(); } } public final double tail() { readLock.lock(); try { checkEmpty(); return tail.val; } finally { readLock.unlock(); } } public final double[] toArray() { readLock.lock(); try { 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; } finally { readLock.unlock(); } } public final void forEach(DoubleConsumer action) { Objects.requireNonNull(action); readLock.lock(); try { if (head != null) head.forEach((Consumer) node -> action.accept(node.val)); } finally { readLock.unlock(); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy