Skip to content

Commit 091b5b1

Browse files
committed
保存源码注释
1 parent c5f92d9 commit 091b5b1

File tree

10 files changed

+140
-96
lines changed

10 files changed

+140
-96
lines changed

src/jdk/java.base/java/util/concurrent/ArrayBlockingQueue.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public class ArrayBlockingQueue<E> extends AbstractQueue<E>
117117
* found in any textbook.
118118
*/
119119

120+
// 一把锁 lock,两个条件 notEmpty + notFull
120121
/** Main lock guarding all access */
121122
final ReentrantLock lock;
122123

@@ -184,7 +185,7 @@ private void enqueue(E e) {
184185
items[putIndex] = e;
185186
if (++putIndex == items.length) putIndex = 0;
186187
count++;
187-
notEmpty.signal();
188+
notEmpty.signal(); // 入队成功,则通知非空条件
188189
}
189190

190191
/**
@@ -203,7 +204,7 @@ private E dequeue() {
203204
count--;
204205
if (itrs != null)
205206
itrs.elementDequeued();
206-
notFull.signal();
207+
notFull.signal(); // 出队成功,则通知非满条件
207208
return e;
208209
}
209210

@@ -271,6 +272,7 @@ public ArrayBlockingQueue(int capacity, boolean fair) {
271272
if (capacity <= 0)
272273
throw new IllegalArgumentException();
273274
this.items = new Object[capacity];
275+
// 在构造函数中初始化锁和条件
274276
lock = new ReentrantLock(fair);
275277
notEmpty = lock.newCondition();
276278
notFull = lock.newCondition();
@@ -367,7 +369,7 @@ public void put(E e) throws InterruptedException {
367369
lock.lockInterruptibly();
368370
try {
369371
while (count == items.length)
370-
notFull.await();
372+
notFull.await(); // 当 put 时,队列已满,阻塞在“非满”条件
371373
enqueue(e);
372374
} finally {
373375
lock.unlock();
@@ -417,7 +419,7 @@ public E take() throws InterruptedException {
417419
lock.lockInterruptibly();
418420
try {
419421
while (count == 0)
420-
notEmpty.await();
422+
notEmpty.await(); // take 时,如果队列为空,则阻塞在“非空”条件上
421423
return dequeue();
422424
} finally {
423425
lock.unlock();

src/jdk/java.base/java/util/concurrent/ConcurrentHashMap.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,13 +2666,13 @@ private final void treeifyBin(Node<K,V>[] tab, int index) {
26662666
Node<K,V> b; int n;
26672667
if (tab != null) {
26682668
if ((n = tab.length) < MIN_TREEIFY_CAPACITY)
2669-
tryPresize(n << 1);
2669+
tryPresize(n << 1); // 数组长度小于阈值 64,则不做红黑树转换,直接扩容
26702670
else if ((b = tabAt(tab, index)) != null && b.hash >= 0) {
2671-
synchronized (b) {
2671+
synchronized (b) { // 链表转换成红黑树
26722672
if (tabAt(tab, index) == b) {
26732673
TreeNode<K,V> hd = null, tl = null;
26742674
for (Node<K,V> e = b; e != null; e = e.next) {
2675-
TreeNode<K,V> p =
2675+
TreeNode<K,V> p = // 遍历链表,构建红黑树
26762676
new TreeNode<K,V>(e.hash, e.key, e.val,
26772677
null, null);
26782678
if ((p.prev = tl) == null)

src/jdk/java.base/java/util/concurrent/CyclicBarrier.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,36 @@ private int dowait(boolean timed, long nanos)
209209
if (g.broken)
210210
throw new BrokenBarrierException();
211211

212-
if (Thread.interrupted()) {
213-
breakBarrier();
212+
if (Thread.interrupted()) { // 响应中断
213+
breakBarrier(); // 唤醒所有阻塞的线程
214214
throw new InterruptedException();
215215
}
216216

217-
int index = --count;
218-
if (index == 0) { // tripped
217+
int index = --count; // 线程每次调用 await 时,count 都减一
218+
if (index == 0) { // tripped // count == 0,则唤醒所有等待线程
219219
Runnable command = barrierCommand;
220220
if (command != null) {
221221
try {
222-
command.run();
222+
command.run(); // 执行回调
223223
} catch (Throwable ex) {
224-
breakBarrier();
224+
breakBarrier(); // 唤醒所有阻塞的线程
225225
throw ex;
226226
}
227227
}
228-
nextGeneration();
228+
nextGeneration(); // 唤醒所有线程,然后把 count 复原,进入下一个迭代。
229229
return 0;
230230
}
231231

232232
// loop until tripped, broken, interrupted, or timed out
233-
for (;;) {
233+
for (;;) { // count > 0,说明没有到齐,则阻塞自己
234234
try {
235235
if (!timed)
236236
trip.await();
237237
else if (nanos > 0L)
238238
nanos = trip.awaitNanos(nanos);
239239
} catch (InterruptedException ie) {
240240
if (g == generation && ! g.broken) {
241-
breakBarrier();
241+
breakBarrier(); // 唤醒所有阻塞的线程
242242
throw ie;
243243
} else {
244244
// We're about to finish waiting even if we had not
@@ -251,11 +251,11 @@ else if (nanos > 0L)
251251
if (g.broken)
252252
throw new BrokenBarrierException();
253253

254-
if (g != generation)
254+
if (g != generation) // 从阻塞中唤醒,返回
255255
return index;
256256

257257
if (timed && nanos <= 0L) {
258-
breakBarrier();
258+
breakBarrier(); // 唤醒所有阻塞的线程
259259
throw new TimeoutException();
260260
}
261261
}

src/jdk/java.base/java/util/concurrent/Phaser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,7 @@ public void forceTermination() {
830830
* @return the phase number, or a negative value if terminated
831831
*/
832832
public final int getPhase() {
833-
return (int)(root.state >>> PHASE_SHIFT);
833+
return (int)(root.state >>> PHASE_SHIFT); // 获取当前轮数
834834
}
835835

836836
/**

src/jdk/java.base/java/util/concurrent/ScheduledThreadPoolExecutor.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ private class ScheduledFutureTask<V>
225225
ScheduledFutureTask(Runnable r, V result, long triggerTime,
226226
long period, long sequenceNumber) {
227227
super(r, result);
228-
this.time = triggerTime;
229-
this.period = period;
230-
this.sequenceNumber = sequenceNumber;
228+
this.time = triggerTime; // 延迟时间
229+
this.period = period; // 周期
230+
this.sequenceNumber = sequenceNumber; // 序列号
231231
}
232232

233233
/**
@@ -255,7 +255,7 @@ public int compareTo(Delayed other) {
255255
return -1;
256256
else if (diff > 0)
257257
return 1;
258-
else if (sequenceNumber < x.sequenceNumber)
258+
else if (sequenceNumber < x.sequenceNumber) // 如果延迟时间相等,则再进一步比较序号
259259
return -1;
260260
else
261261
return 1;
@@ -275,12 +275,15 @@ public boolean isPeriodic() {
275275

276276
/**
277277
* Sets the next time to run for a periodic task.
278+
* 设置下次执行时间
278279
*/
279280
private void setNextRunTime() {
280281
long p = period;
281282
if (p > 0)
283+
// AtFixedRate 时,p > 0,下次执行时间是上次任务开始时间 + period,即固定周期
282284
time += p;
283285
else
286+
// WithFixedDelay 时,p < 0,下次执行时间是上次任务结束时间 + period,即固定延迟
284287
time = triggerTime(-p);
285288
}
286289

@@ -300,10 +303,11 @@ public boolean cancel(boolean mayInterruptIfRunning) {
300303
public void run() {
301304
if (!canRunInCurrentRunState(this))
302305
cancel(false);
303-
else if (!isPeriodic())
306+
else if (!isPeriodic()) // 不是周期性的,则执行一次就结束
304307
super.run();
305308
else if (super.runAndReset()) {
306-
setNextRunTime();
309+
// 周期性的任务,执行完之后,重新计算延迟时间,然后再仍会队列
310+
setNextRunTime(); // 设置下次执行时间
307311
reExecutePeriodic(outerTask);
308312
}
309313
}
@@ -339,7 +343,7 @@ private void delayedExecute(RunnableScheduledFuture<?> task) {
339343
if (isShutdown())
340344
reject(task);
341345
else {
342-
super.getQueue().add(task);
346+
super.getQueue().add(task); // 将 ScheduledFutureTask 对象添加到等待队列
343347
if (!canRunInCurrentRunState(task) && remove(task))
344348
task.cancel(false);
345349
else
@@ -625,7 +629,7 @@ public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
625629
new ScheduledFutureTask<Void>(command,
626630
null,
627631
triggerTime(initialDelay, unit),
628-
unit.toNanos(period),
632+
unit.toNanos(period), // 注意:正数
629633
sequencer.getAndIncrement());
630634
RunnableScheduledFuture<Void> t = decorateTask(command, sft);
631635
sft.outerTask = t;
@@ -673,7 +677,7 @@ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
673677
new ScheduledFutureTask<Void>(command,
674678
null,
675679
triggerTime(initialDelay, unit),
676-
-unit.toNanos(delay),
680+
-unit.toNanos(delay), // 注意:负数
677681
sequencer.getAndIncrement());
678682
RunnableScheduledFuture<Void> t = decorateTask(command, sft);
679683
sft.outerTask = t;

0 commit comments

Comments
 (0)