本文共 7531 字,大约阅读时间需要 25 分钟。
SynchronousQueue ? Java ?????????????????????????????????????????????????????????? TransferStack ???????? TransferQueue??????????????? SynchronousQueue ????????
SynchronousQueue ???? AbstractQueue????? BlockingQueue ? java.io.Serializable ?????????????????????????????????????????? fair ???
public class SynchronousQueue extends AbstractQueue implements BlockingQueue, java.io.Serializable { private transient volatile Transferer transferer; public SynchronousQueue(boolean fair) { transferer = fair ? new TransferQueue() : new TransferStack(); } // ... ???? ...} transfer ???????????????????? TransferStack ? TransferQueue???????? put(E e) ???????? take() ????? transferer.transfer ?????????????????
public void put(E e) throws InterruptedException { if (e == null) throw new NullPointerException(); if (transferer.transfer(e, false, 0) == null) { Thread.interrupted(); throw new InterruptedException(); }}public E take() throws InterruptedException { E e = transferer.transfer(null, false, 0); if (e != null) return e; Thread.interrupted(); throw new InterruptedException();} put ???????????????????????????????????????????take ???????????????????????????????????TransferStack ??????????????????? SNode ????????????? SNode ?????
static final class SNode { volatile SNode next; volatile SNode match; volatile Thread waiter; Object item; int mode; SNode(Object item) { this.item = item; } boolean casNext(SNode cmp, SNode val) { return cmp == next && UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); } boolean tryMatch(SNode s) { if (match == null && UNSAFE.compareAndSwapObject(this, matchOffset, null, s)) { Thread w = waiter; if (w != null) { waiter = null; LockSupport.unpark(w); } return true; } return match == s; } void tryCancel() { UNSAFE.compareAndSwapObject(this, matchOffset, null, this); } boolean isCancelled() { return match == this; }} next ???????????? match ???????????? waiter ??????????Unsafe ???????????????????????TransferQueue ???????????????????? QNode ?????????????? QNode ?????
static final class QNode { volatile QNode next; volatile Object item; volatile Thread waiter; final boolean isData; QNode(Object item, boolean isData) { this.item = item; this.isData = isData; } boolean casNext(QNode cmp, QNode val) { return next == cmp && UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val); } boolean casItem(Object cmp, Object val) { return item == cmp && UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); } void tryCancel(Object cmp) { UNSAFE.compareAndSwapObject(this, itemOffset, cmp, this); } boolean isCancelled() { return item == this; } boolean isOffList() { return next == this; }} head ? tail ??????????? cleanMe ????????????Unsafe ?????????????????????????TransferStack ? transfer ???????????????????
E transfer(E e, boolean timed, long nanos) { SNode s = null; int mode = (e == null) ? REQUEST : DATA; for (;;) { SNode h = head; if (h == null || h.mode == mode) { if (timed && nanos <= 0) { if (h != null && h.isCancelled()) { casHead(h, h.next); } else { return null; } } if (casHead(h, s = snode(s, e, h, mode))) { SNode m = awaitFulfill(s, timed, nanos); if (m == s) { clean(s); return null; } if ((h = head) != null && h.next == s) { casHead(h, s.next); } return (m != null) ? (E) m.item : s.item; } if (!isFulfilling(h.mode)) { if (h.isCancelled()) { casHead(h, h.next); } else if (casHead(h, s = snode(s, e, h, FULFILLING | mode))) { for (;;) { SNode m = s.next; if (m == null) { casHead(s, null); s = null; break; } SNode mn = m.next; if (m.tryMatch(s)) { casHead(s, mn); return (m != null) ? (E) m.item : s.item; } else { s.casNext(m, mn); } } } } else { SNode m = h.next; if (m == null) { casHead(h, null); } else { SNode mn = m.next; if (m.tryMatch(h)) { casHead(h, mn); } else { h.casNext(m, mn); } } } } else if (!isFulfilling(h.mode)) { // ... ????? ... } }} put ?????????????? casHead ?????????take ??????????????? casHead ?????????TransferQueue ? transfer ?????????????????????
@SuppressWarnings("unchecked")E transfer(E e, boolean timed, long nanos) { QNode s = null; boolean isData = (e != null); for (;;) { QNode t = tail; QNode h = head; if (t == null || h == null) continue; if (h == t || t.isData == isData) { QNode tn = t.next; if (t != tail) continue; if (tn != null) { advanceTail(t, tn); continue; } if (timed && nanos <= 0) return null; if (s == null) s = new QNode(e, isData); if (!t.casNext(null, s)) continue; advanceTail(t, s); Object x = awaitFulfill(s, e, timed, nanos); if (x == s) { clean(t, s); return null; } if (!s.isOffList()) { advanceHead(t, s); if (x != null) s.item = s; s.waiter = null; } return (x != null) ? (E) x : e; } else { QNode m = h.next; if (t != tail || m == null || h != head) continue; Object x = m.item; if (isData == (x != null) || x == m || !m.casItem(x, e)) { advanceHead(h, m); continue; } advanceHead(h, m); LockSupport.unpark(m.waiter); return (x != null) ? (E) x : e; } }} put ???????????????? advanceTail ??????????take ????????????????? advanceHead ??????????SynchronousQueue ????????????????????????????????? TransferStack ????????? TransferQueue????????????????????????????????????????????? SynchronousQueue ?????????????????????????????????
转载地址:http://uxmr.baihongyu.com/