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

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

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

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

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

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

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





© 2015 - 2024 Weber Informatics LLC | Privacy Policy