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

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

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

import java.util.concurrent.atomic.AtomicLongFieldUpdater;
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

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

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

* B=16+24*n */ @SuppressWarnings("restriction") public class ConcurrentJDNode implements DNode { private static final long serialVersionUID = 6121417618430692988L; public volatile long val; protected volatile ConcurrentJDNode next; protected volatile ConcurrentJDNode prev; protected ConcurrentJDNode(long val) { this.val = val; } public static ConcurrentJDNode of(long e) { return new ConcurrentJDNode(e); } public static ConcurrentJDNode of() { return of(0L); } private static final AtomicLongFieldUpdater valUpdater = AtomicLongFieldUpdater.newUpdater(ConcurrentJDNode.class, "val"); private static final AtomicReferenceFieldUpdater nextUpdater = AtomicReferenceFieldUpdater.newUpdater(ConcurrentJDNode.class, ConcurrentJDNode.class, "next"); private static final AtomicReferenceFieldUpdater prevUpdater = AtomicReferenceFieldUpdater.newUpdater(ConcurrentJDNode.class, ConcurrentJDNode.class, "prev"); public boolean casVal(long expect, long update) { return valUpdater.compareAndSet(this, expect, update); } public boolean casNext(ConcurrentJDNode expect, ConcurrentJDNode update) { return nextUpdater.compareAndSet(this, expect, update); } public boolean casPrev(ConcurrentJDNode expect, ConcurrentJDNode update) { return prevUpdater.compareAndSet(this, expect, update); } @Override public final ConcurrentJDNode next() { return next; } @Override public final void next(ConcurrentJDNode node) { next = node; } @Override public final ConcurrentJDNode prev() { return prev; } @Override public final void prev(ConcurrentJDNode node) { prev = node; } @Override public final ConcurrentJDNode addNext(ConcurrentJDNode node) { while (true) { ConcurrentJDNode 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 ConcurrentJDNode addPrev(ConcurrentJDNode node) { while (true) { ConcurrentJDNode 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() { ConcurrentJDNode prev = this.prev; ConcurrentJDNode 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 ConcurrentJDNode addNext(long e) { return addNext(of(e)); } public final ConcurrentJDNode addNext(long... es) { ConcurrentJDNode next = this; for (long e : es) next = next.addNext(e); return next; } public final ConcurrentJDNode addPrev(long e) { return addPrev(of(e)); } public final ConcurrentJDNode addPrev(long... es) { ConcurrentJDNode prev = this; for (long e : es) prev = prev.addPrev(e); return prev; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy