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

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

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/

你可能感兴趣的文章
npm ERR! Unexpected end of JSON input while parsing near '...on":"0.10.3","direc to'
查看>>
npm ERR! Unexpected end of JSON input while parsing near ‘...“:“^1.2.0“,“vue-html-‘ npm ERR! A comp
查看>>
npm error Missing script: “server“npm errornpm error Did you mean this?npm error npm run serve
查看>>
npm error MSB3428: 未能加载 Visual C++ 组件“VCBuild.exe”。要解决此问题,1) 安装
查看>>
npm install CERT_HAS_EXPIRED解决方法
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 Failed to connect to github.com port 443 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>