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

shz.core.node.ConcurrentDDNode Maven / Gradle / Ivy

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

import shz.core.UnsafeHelp;

/**
 * 元素类型为double支持并发的双向链表节点
 * 

* 24*n(n为元素个数) *

* B=16+24*n */ @SuppressWarnings({"restriction"}) public class ConcurrentDDNode implements DNode { public volatile double val; protected volatile ConcurrentDDNode next; protected volatile ConcurrentDDNode prev; protected ConcurrentDDNode(double val) { this.val = val; } public static ConcurrentDDNode of(double e) { return new ConcurrentDDNode(e); } public static ConcurrentDDNode of() { return of(0d); } private static final long valOffset; private static final long nextOffset; private static final long prevOffset; static { try { Class k = ConcurrentDDNode.class; valOffset = UnsafeHelp.getUnsafe().objectFieldOffset(k.getDeclaredField("val")); nextOffset = UnsafeHelp.getUnsafe().objectFieldOffset(k.getDeclaredField("next")); prevOffset = UnsafeHelp.getUnsafe().objectFieldOffset(k.getDeclaredField("prev")); } catch (Exception e) { throw new Error(e); } } public boolean casVal(double expect, double val) { return UnsafeHelp.getUnsafe().compareAndSwapObject(this, valOffset, expect, val); } public boolean casNext(ConcurrentDDNode expect, ConcurrentDDNode next) { return UnsafeHelp.getUnsafe().compareAndSwapObject(this, nextOffset, expect, next); } public boolean casPrev(ConcurrentDDNode expect, ConcurrentDDNode prev) { return UnsafeHelp.getUnsafe().compareAndSwapObject(this, prevOffset, expect, prev); } @Override public final ConcurrentDDNode next() { return next; } @Override public final void next(ConcurrentDDNode node) { next = node; } @Override public final ConcurrentDDNode prev() { return prev; } @Override public final void prev(ConcurrentDDNode node) { prev = node; } @Override public final ConcurrentDDNode addNext(ConcurrentDDNode node) { while (true) { ConcurrentDDNode next = this.next; node.prev = this; node.next = next; if (!casNext(next, node)) continue; if (next != null) next.prev = node; break; } return node; } @Override public final ConcurrentDDNode addPrev(ConcurrentDDNode node) { while (true) { ConcurrentDDNode prev = this.prev; node.prev = prev; node.next = this; if (!casPrev(prev, node)) continue; if (prev != null) prev.next = node; break; } return node; } @Override public final void poll() { ConcurrentDDNode prev = this.prev; ConcurrentDDNode next = this.next; if (prev != null) { if (prev.casNext(this, next)) if (next != null) next.prev = prev; } else if (next != null) next.casPrev(this, null); } public final ConcurrentDDNode addNext(double e) { return addNext(of(e)); } public final ConcurrentDDNode addNext(double... es) { ConcurrentDDNode next = this; for (double e : es) next = next.addNext(e); return next; } public final ConcurrentDDNode addPrev(double e) { return addPrev(of(e)); } public final ConcurrentDDNode addPrev(double... es) { ConcurrentDDNode prev = this; for (double e : es) prev = prev.addPrev(e); return prev; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy