Java 并发编程中的 CLH 队列
英文句子 | 中文译文 | 生词/专业词标注与翻译 | 句子结构拆解 |
---|---|---|---|
Insertion into a CLH queue requires only a single atomic operation on “tail”, so there is a simple atomic point of demarcation from unqueued to queued. | 向 CLH 队列插入仅需要对“tail”执行一次原子操作,因此存在一个从未入队到入队的简单原子分界点。 | - Insertion:插入(名词,指“将元素加入队列”的动作) - atomic operation:原子操作(不可中断的操作,要么全执行要么全不执行) - tail:尾部(此处指队列的尾指针) - demarcation:分界点(区分两种状态的标志) - unqueued:未入队的(形容词,“un-”是否定前缀,“queued”是“入队的”) | 主句1(Insertion… requires… operation)+ 结果状语从句(so there is… point)。主句1说明“插入队列”的操作特点,从句说明由此带来的“状态分界”结果。 |
Similarly, dequeuing involves only updating the “head”. | 类似地,出队仅涉及更新“head”。 | - dequeuing:出队(名词,“de-”表示“去除”,指“将元素从队列移除”的动作) - involves:涉及(动词,此处指“出队动作需要做的事”) - head:头部(此处指队列的头指针) | 简单句,“dequeuing”是主语,“involves”是谓语,“updating the ‘head’”是宾语,说明出队的核心操作。 |
However, it takes a bit more work for nodes to determine who their successors are, in part to deal with possible cancellation due to timeouts and interrupts. | 不过,节点要确定自己的后继需要多做一些工作,部分原因是为了处理因超时和中断导致的可能取消情况。 | - successors:后继(名词,指“队列中排在当前节点后面的节点”) - in part:部分原因(副词短语,用于解释“多做工作”的理由) - cancellation:取消(名词,指“节点放弃等待”的状态) - timeouts:超时(名词,指“等待时间超过限制”) - interrupts:中断(名词,指“外部强制暂停线程”的操作) | 主句(it takes… work for nodes to determine…)+ 目的状语(in part to deal with…)。“it”是形式主语,真正主语是“to determine who their successors are”;目的状语解释“多做工作”的用途。 |
The “prev” links (not used in original CLH locks), are mainly needed to handle cancellation. | “prev”链接(在原始 CLH 锁中未使用)主要用于处理取消。 | - prev links:prev链接(“prev”是“previous”的缩写,指“当前节点的前驱节点链接”) - original:原始的(形容词,指“最初版本的 CLH 锁”) - handle:处理(动词,此处指“应对取消状态”) | 简单句,主语是“The ‘prev’ links”,括号内是插入语(补充说明prev链接的历史),“are needed”是谓语,“to handle cancellation”是目的状语。 |
If a node is cancelled, its successor is (normally) relinked to a non-cancelled predecessor. | 如果一个节点被取消,它的后继(通常)会重新链接到一个未被取消的前驱。 | - relinked:重新链接(动词,“re-”表示“重新”,指“修改节点的链接指向”) - non-cancelled:未被取消的(形容词,“non-”是否定前缀) - predecessor:前驱(名词,指“队列中排在当前节点前面的节点”) | 条件状语从句(If a node is cancelled)+ 主句(its successor is relinked…)。从句说明“触发条件”,主句说明“条件满足时的操作”。 |
For explanation of similar mechanics in the case of spin locks, see the papers by Scott and Scherer at http://www.cs.rochester.edu/u/scott/synchronization/ | 关于自旋锁中类似机制的解释,可参见 Scott 和 Scherer 在 http://www.cs.rochester.edu/u/scott/synchronization/ 上的论文。 | - mechanics:机制(名词,指“系统运作的方式”) - spin locks:自旋锁(一种锁机制,线程会循环等待锁释放,不阻塞) - papers:论文(名词,此处指“学术研究文献”) | 祈使句,“For explanation of…”是目的状语(说明“看论文”的用途),“see”是谓语,“the papers”是宾语,后面接作者和链接(补充论文信息)。 |
We also use “next” links to implement blocking mechanics. | 我们还使用“next”链接来实现阻塞机制。 | - next links:next链接(“next”指“当前节点的后继节点链接”) - implement:实现(动词,指“将机制落地为代码”) - blocking mechanics:阻塞机制(线程等待时会暂停的机制,区别于自旋锁) | 简单句,“We”是主语,“use”是谓语,“‘next’ links”是宾语,“to implement blocking mechanics”是目的状语(说明用next链接做什么)。 |
The thread id for each node is kept in its own node, so a predecessor signals the next node to wake up by traversing next link to determine which thread it is. | 每个节点的线程 ID 保存在其自身节点中,因此前驱通过遍历 next 链接来确定是哪个线程,从而向后续节点发信号唤醒。 | - thread id:线程ID(唯一标识线程的编号) - kept:保存(动词,“keep”的过去分词,指“存储”) - signals:发信号(动词,指“通知线程可以继续”) - wake up:唤醒(动词短语,指“让暂停的线程恢复运行”) - traversing:遍历(动词,指“沿着链接找到下一个节点”) | 主句1(The thread id… is kept…)+ 结果状语从句(so a predecessor signals…)。从句中,“by traversing… to determine…”是方式状语(说明“发信号”的步骤)。 |
Determination of successor must avoid races with newly queued nodes to set the “next” fields of their predecessors. | 后继的确定必须避免与新入队的节点竞争以设置其前驱的“next”字段。 | - Determination of successor:后继的确定(名词短语,指“找到后继节点的过程”) - races:竞争(名词,指“多个线程同时操作同一资源的冲突”) - newly queued nodes:新入队的节点(“newly”是副词,修饰“queued”) - fields:字段(计算机术语,指“类中的属性”,此处指节点的next属性) | 简单句,“Determination of successor”是主语,“must avoid”是谓语,“races”是宾语,“with newly queued nodes to set…”是定语(说明“竞争”的对象和目的)。 |
This is solved when necessary by checking backwards from the atomically updated “tail” so that we don’t succeed when a node’s successor appears to be null. | 必要时,通过从原子更新的“tail”向后检查来解决这个问题,这样当一个节点的后继看似为空时,我们不会成功。 | - solved:解决(动词,“solve”的过去分词) - checking backwards:向后检查(指“从尾指针往头指针方向找节点”) - atomically updated:原子更新的(指“tail的修改是不可中断的”) - appears to be null:看似为空(指“表面上没有后继,但可能是还没更新完”) | 主句(This is solved… by checking…)+ 目的状语从句(so that we don’t succeed…)。“when necessary”是时间状语(说明“解决时机”),从句中“when a node’s successor…”是条件状语(说明“不成功”的条件)。 |
(Or, said differently, the next-links are an optimization so that we don’t usually need a backward scan.) | (或者说,next 链接是一种优化,因此我们通常不需要向后扫描。) | - optimization:优化(名词,指“让操作更高效的设计”) - backward scan:向后扫描(和“checking backwards”同义,指“从尾往头找节点”) | 插入语(补充说明前一句),主句(the next-links are an optimization)+ 结果状语从句(so that we don’t need…)。 |
Cancellation introduces some conservatism to the basic algorithms. | 取消给基本算法带来了一些保守性。 | - introduces:带来(动词,指“使算法增加某种特性”) - conservatism:保守性(名词,此处指“算法为了应对取消,会采用更稳妥、不冒进的逻辑”) - basic algorithms:基本算法(指“CLH队列的核心处理逻辑”) | 简单句,“Cancellation”是主语,“introduces”是谓语,“some conservatism”是宾语,“to the basic algorithms”是状语(说明“带来保守性”的对象)。 |
Since we must poll for cancellation of other nodes, we can miss noticing whether a cancelled node is still ahead or behind us. | 由于我们必须轮询其他节点的取消情况,可能会遗漏注意到已取消的节点是在我们前面还是后面。 | - poll:轮询(动词,指“反复检查节点状态”) - miss noticing:遗漏注意(动词短语,指“没发现”) - ahead:在前面(副词,指“队列中排在当前节点之前”) - behind:在后面(副词,指“队列中排在当前节点之后”) | 原因状语从句(Since we must poll…)+ 主句(we can miss noticing…)。主句中,“whether a cancelled node… or…”是宾语从句(说明“遗漏注意”的内容)。 |
This is dealt with by always unparking successors upon cancellation, allowing them to stabilize on a new predecessor, unless we can identify an uncancelled predecessor who will carry this responsibility. | 这通过在取消时始终解除后继的停放来处理,允许它们稳定在新的前驱上,除非我们能确定一个未被取消的前驱来承担这个责任。 | - dealt with:处理(动词短语,“deal with”的被动形式) - unparking:解除停放(计算机术语,指“允许被暂停的线程继续运行”,对应“parking(停放)”) - stabilize:稳定(动词,指“让节点找到确定的新前驱,不再变动”) - carry this responsibility:承担这个责任(指“由前驱节点负责唤醒后继”) | 主句(This is dealt with by…)+ 伴随状语(allowing them to…)+ 条件状语从句(unless we can identify…)。“by always unparking…”是方式状语(说明“处理”的手段)。 |
CLH queues need a dummy header node to get started. | CLH 队列需要一个虚拟头节点来启动。 | - dummy header node:虚拟头节点(“dummy”指“虚假的、占位的”,启动时用的空节点,不是真正的等待节点) - get started:启动(动词短语,指“队列开始运作”) | 简单句,“CLH queues”是主语,“need”是谓语,“a dummy header node”是宾语,“to get started”是目的状语(说明“需要虚拟头节点”的用途)。 |
But we don’t create them on construction, because it would be wasted effort if there is never contention. | 但我们不在构造时创建它,因为如果从未有竞争,这会是白费力气。 | - on construction:在构造时(指“创建CLH队列对象的时候”) - wasted effort:白费力气(指“做了没用的工作”) - contention:竞争(名词,指“多个线程同时争夺资源,需要排队的情况”) | 主句(we don’t create them…)+ 原因状语从句(because it would be…)。从句中,“if there is never contention”是条件状语(说明“白费力气”的条件)。 |
Instead, the node is constructed and head and tail pointers are set upon first contention. | 相反,节点在首次竞争时被构造,并且头和尾指针被设置。 | - constructed:构造(动词,“construct”的过去分词,指“创建节点对象”) - pointers:指针(此处指“指向队列头、尾节点的引用”) - upon first contention:在首次竞争时(指“第一次出现线程排队的情况时”) | 并列句,由“and”连接两个被动句(the node is constructed / head and tail pointers are set),“upon first contention”是时间状语(说明“构造和设置”的时机)。 |
Threads waiting on Conditions use the same nodes, but use an additional link. | 等待条件的线程使用相同的节点,但使用额外的链接。 | - waiting on Conditions:等待条件的(现在分词短语,指“线程在等待某个条件满足,比如‘队列不为空’”) - Conditions:条件(Java中的Condition接口,用于线程间按条件唤醒) - additional link:额外的链接(指“除了prev、next之外,专门用于条件等待的链接”) | 并列句,由“but”连接两个分句(Threads… use the same nodes / (threads) use an additional link),前一个分句中“waiting on Conditions”是定语(修饰“Threads”)。 |
Conditions only need to link nodes in simple (non-concurrent) linked queues because they are only accessed when exclusively held. | 条件仅需要在简单(非并发)的链接队列中链接节点,因为它们仅在独占持有时被访问。 | - non-concurrent:非并发的(形容词,指“同一时间只有一个线程操作”) - linked queues:链接队列(用节点和链接组成的队列) - accessed:访问(动词,指“读取或修改队列”) - exclusively held:独占持有(指“只有一个线程能拥有访问权限”) | 主句(Conditions only need to link…)+ 原因状语从句(because they are accessed…)。从句中“when exclusively held”是时间状语(说明“访问”的前提)。 |
Upon await, a node is inserted into a condition queue. | 在 await 时,一个节点被插入到条件队列中。 | - Upon await:在await时(“upon”是“在……时”,“await”是Condition的方法,指“线程等待条件,进入暂停状态”) - condition queue:条件队列(专门存储“等待条件的节点”的队列) | 简单句,“Upon await”是时间状语,“a node”是主语,“is inserted”是谓语,“into a condition queue”是状语(说明“插入”的目标)。 |
Upon signal, the node is transferred to the main queue. | 在 signal 时,该节点被转移到主队列中。 | - Upon signal:在signal时(“signal”是Condition的方法,指“通知等待条件的线程,条件已满足”) - transferred:转移(动词,指“从条件队列移到CLH主队列”) - main queue:主队列(CLH队列的核心队列,用于线程排队等待资源) | 简单句,结构和上一句一致:时间状语(Upon signal)+ 主语(the node)+ 谓语(is transferred)+ 目标状语(to the main queue)。 |
A special value of status field is used to mark which queue a node is on. | status 字段的一个特殊值用于标记节点所在的队列。 | - status field:status字段(节点中的属性,用于存储节点状态,如“等待中”“已取消”) - mark:标记(动词,指“用值区分状态”) - which queue a node is on:节点所在的队列(宾语从句,说明“标记”的内容) | 简单句,“A special value of status field”是主语,“is used”是谓语,“to mark which queue…”是目的状语(说明“用特殊值做什么”)。 |
Thanks go to Dave Dice, Mark Moir, Victor Luchangco, Bill Scherer and Michael Scott, along with members of JSR-166 expert group, for helpful ideas, discussions, and critiques on the design of this class. | 感谢 Dave Dice、Mark Moir、Victor Luchangco、Bill Scherer 和 Michael Scott,以及 JSR-166 专家组的成员,他们对该类的设计提出了有益的想法、进行了讨论并提出了批评。 | - Thanks go to:感谢(固定搭配,“go to”表示“归于”) - JSR-166 expert group:JSR-166专家组(JSR是“Java规范请求”,JSR-166负责制定Java并发编程规范) - critiques:批评(名词,指“建设性的意见反馈”) - design of this class:该类的设计(指“CLH队列相关的Java类,如AbstractQueuedSynchronizer”) | 简单句,“Thanks”是主语,“go to”是谓语,后面接感谢的对象(人名和专家组),“for helpful ideas… critiques”是目的状语(说明“感谢”的原因)。 |
static final class Node { | 静态最终类 Node { | - static final class:静态最终类(“static”表示“不属于外部类实例”,“final”表示“不能被继承”,“class”是“类”) - Node:类名(此处指CLH队列中的“节点类”) | 类定义语句,“static final”是类的修饰符,“Node”是类名,大括号内是类的内容。 |
Marker to indicate a node is waiting in shared mode | 标记节点以指示其以共享模式等待 | - Marker:标记(名词,指“用于区分状态的标识”) - indicate:指示(动词,指“表明节点的状态”) - shared mode:共享模式(线程以“共享”方式获取资源,如多个线程可同时读数据) | 名词短语,“to indicate a node is waiting…”是定语(说明“标记”的用途),“in shared mode”是状语(说明“等待”的方式)。 |
static final Node SHARED = new Node(); | 静态最终 Node SHARED = new Node(); | - static final Node:静态最终Node对象(“static final”修饰对象,表示“全局唯一、不可修改”) - SHARED:对象名(表示“共享模式”的标记对象) - new Node():创建Node对象(“new”是“创建对象”的关键字) | 变量定义语句,“static final Node”是变量类型,“SHARED”是变量名,“new Node()”是变量的初始值。 |
Marker to indicate a node is waiting in exclusive mode | 标记节点以指示其以独占模式等待 | - exclusive mode:独占模式(线程以“独占”方式获取资源,如同一时间只有一个线程可写数据,区别于共享模式) | 名词短语,结构和“共享模式”那句一致:“to indicate a node is waiting…”是定语,“in exclusive mode”是状语(说明“等待”的方式)。 |
关键背景补充
这些内容都围绕 Java 并发编程中的 CLH 队列(用于实现锁、信号量等同步工具)展开,核心是解释“节点如何在队列中排队、如何处理取消、如何与条件等待配合”。对大一学生来说,重点先记住3个核心概念:
- 节点(Node):队列的基本单位,存储线程信息和状态(如SHARED/exclusive模式);
- prev/next链接:节点间的“前后指针”,prev处理取消,next实现唤醒;
- 共享/独占模式:共享模式支持多线程同时访问,独占模式只允许一个线程访问,这是Java锁(如ReentrantLock)的核心逻辑。
要不要我帮你整理一份 CLH队列核心概念对照表?把文中的专业词、英文、中文翻译和简单解释汇总在一起,方便你复习时快速查阅。