博客
关于我
【JDK源码分析系列】SynchronousQueue源码分析
阅读量:363 次
发布时间:2019-03-04

本文共 7348 字,大约阅读时间需要 24 分钟。

SynchronousQueue ????

SynchronousQueue ? Java ?????????????????????????????????????????????????????????? TransferStack ???????? TransferQueue??????????????? SynchronousQueue ????????

1. ????

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();    }    // ... ???? ...}
  • Transferer ???????????????? transfer ???????????????????? TransferStack ? TransferQueue?

2. ???????

??????? 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 ???????????????????????????????????

3. ??????

3.1 TransferStack ??

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 ??????????
  • CAS ????? Unsafe ???????????????????????

3.2 TransferQueue ??

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 ?????????????????????????

4. ????

4.1 TransferStack ? transfer ??

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 ?????????
  • ????????????????????????????????????

4.2 TransferQueue ? transfer ??

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 ??????????
  • ????????????????????????????????????

5. ??

SynchronousQueue ????????????????????????????????? TransferStack ????????? TransferQueue????????????????????????????????????????????? SynchronousQueue ?????????????????????????????????

转载地址:http://uxmr.baihongyu.com/

你可能感兴趣的文章
node中fs模块之文件操作
查看>>
Node中同步与异步的方式读取文件
查看>>
Node中的Http模块和Url模块的使用
查看>>
Node中自启动工具supervisor的使用
查看>>
Node入门之创建第一个HelloNode
查看>>
node全局对象 文件系统
查看>>
Node出错导致运行崩溃的解决方案
查看>>
Node响应中文时解决乱码问题
查看>>
node基础(二)_模块以及处理乱码问题
查看>>
node安装卸载linux,Linux运维知识之linux 卸载安装node npm
查看>>
node安装及配置之windows版
查看>>
Node实现小爬虫
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
Node提示:npm does not support Node.js v12.16.3
查看>>
Node搭建静态资源服务器时后缀名与响应头映射关系的Json文件
查看>>
Node服务在断开SSH后停止运行解决方案(创建守护进程)
查看>>
node模块化
查看>>
node模块的本质
查看>>
node环境下使用import引入外部文件出错
查看>>
node环境:Error listen EADDRINUSE :::3000
查看>>