千家信息网

java中SynchronousQueue的公平和非公平同步队列

发表于:2025-12-02 作者:千家信息网编辑
千家信息网最后更新 2025年12月02日,本篇内容主要讲解"java中SynchronousQueue的公平和非公平同步队列",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"java中Synchron
千家信息网最后更新 2025年12月02日java中SynchronousQueue的公平和非公平同步队列

本篇内容主要讲解"java中SynchronousQueue的公平和非公平同步队列",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"java中SynchronousQueue的公平和非公平同步队列"吧!

SynchronousQueue 分为公平和非公平同步队列
构造方法中传入true,表示公平队列这时选择通过TransferQueue中的方法实现出入元素FIFO
否则传入false,使用TransferStack实现出入元素,没有固定顺序

TransfererStack中的节点组成
static final class SNode{
volatile SNode next;
volatile SNode match;
volatile Thread waiter;
Object item;
int mode;

volatile SNode head;
}


static {
try {
UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = SNode.class;
matchOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("match"));
nextOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("next"));
} catch (Exception e) {
throw new Error(e);
}
}

对 transfer方法的实现
E transfer(E e, boolean timed, long nanos) {
/*
* Basic algorithm is to loop trying one of three actions:
*
* 1. If apparently empty or already containing nodes of same
* mode, try to push node on stack and wait for a match,
* returning it, or null if cancelled.
*
* 2. If apparently containing node of complementary mode,
* try to push a fulfilling node on to stack, match
* with corresponding waiting node, pop both from
* stack, and return matched item. The matching or
* unlinking might not actually be necessary because of
* other threads performing action 3:
*
* 3. If top of stack already holds another fulfilling node,
* help it out by doing its match and/or pop
* operations, and then continue. The code for helping
* is essentially the same as for fulfilling, except
* that it doesn't return the item.
*/

SNode s = null; // constructed/reused as needed
int mode = (e == null) ? REQUEST : DATA;

for (;;) {
SNode h = head;
if (h == null || h.mode == mode) { // empty or same-mode
if (timed && nanos <= 0) { // can't wait
if (h != null && h.isCancelled())
casHead(h, h.next); // pop cancelled node
else
return null;
} else if (casHead(h, s = snode(s, e, h, mode))) {
SNode m = awaitFulfill(s, timed, nanos);
if (m == s) { // wait was cancelled
clean(s);
return null;
}
if ((h = head) != null && h.next == s)
casHead(h, s.next); // help s's fulfiller
return (E) ((mode == REQUEST) ? m.item : s.item);
}
} else if (!isFulfilling(h.mode)) { // try to fulfill
if (h.isCancelled()) // already cancelled
casHead(h, h.next); // pop and retry
else if (casHead(h, s=snode(s, e, h, FULFILLING|mode))) {
for (;;) { // loop until matched or waiters disappear
SNode m = s.next; // m is s's match
if (m == null) { // all waiters are gone
casHead(s, null); // pop fulfill node
s = null; // use new node next time
break; // restart main loop
}
SNode mn = m.next;
if (m.tryMatch(s)) {
casHead(s, mn); // pop both s and m
return (E) ((mode == REQUEST) ? m.item : s.item);
} else // lost match
s.casNext(m, mn); // help unlink
}
}
} else { // help a fulfiller
SNode m = h.next; // m is h's match
if (m == null) // waiter is gone
casHead(h, null); // pop fulfilling node
else {
SNode mn = m.next;
if (m.tryMatch(h)) // help match
casHead(h, mn); // pop both h and m
else // lost match
h.casNext(m, mn); // help unlink
}
}
}
}


TransferQueue中的节点组成

static final class QNode{
volatile QNode next;
volatile Object item;
volatile Thread waiter;
final boolean isData;
transient volatile QNode head;
transient volatile QNode tail;

}

到此,相信大家对"java中SynchronousQueue的公平和非公平同步队列"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

队列 同步 方法 元素 内容 节点 学习 实用 更深 兴趣 实用性 实际 操作简单 更多 朋友 网站 顺序 频道 查询 选择 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 搭建游戏服务器可以干什么 网络安全工程师要会编程吗 网络安全法单选题及答案 网络安全的利与弊议论文 城阳区游戏软件开发公司 公众号5g互联网科技 cmd界面为什么无法打开数据库 重庆百信服务器厂家 网上书店软件开发项目进度 谈谈你对计算机网络安全 网络安全与执法主任论坛 车载通信软件开发工程师 sci数据库检索三种方式 软件开发计税规定 计算机网络技术出去上什么班 数据库怎么更改等级 2021年网络安全答题答案 杭州应用软件开发大概多少钱 服务器虚拟化排名 金融公司融资信息基础数据库 中国网络安全机构 数据库技术考啥 csgo杭州服务器无限掉线 小米软件开发上班真实感受 无法连接到用户管理服务器 空间数据库的数据类型有哪些 中国流行软件开发 如何访问zkt打卡机数据库 皖北经济技术学校网络安全教育 语言学判断数据库
0