0%

Guarded Suspension 保护性暂挂模式

Guarded Suspension 保护性暂挂

当持有锁时,如果条件不满足,暂时挂起执行线程,等待条件满足时再次唤醒,判断条件后,执行操作。

伪代码

1
2
3
4
5
6
7
8
9
10
Lock lock = new Lock();
Conditon condition = lock.newCondition()
...
lock.lock();
while ( conditon not satisfied ) {
conditon.await()
}
doOperation();
lock.unLock()

Sequence Diagram

内部逻辑,图参考自<<Java多线程编程实战指南(设计模式篇)>> 黄文海 著

ClientClientGuardedObjectGuardedObjectGuardedActionGuardedActionBlockerBlockerPredicatePredicateConditionCondition1 : guardedMethod()2 : <<create>>3 : callWithGuard()getGuard()5: predicateloop[SuspendIfNecessary]6 : evaluate()7 : isPredicateTrue8 : await()9 :call()10 : actionReturnValue11 : actionReturnValue

Welcome to my other publishing channels