State

  • volatile修饰
  • 同步状态
  1. //同步状态
  2. private volatile int state;
  3. /**
  4. * Returns the current value of synchronization state.
  5. * This operation has memory semantics of a {@code volatile} read.
  6. * @return current state value
  7. */
  8. protected final int getState() {
  9. return state;
  10. }
  11. /**
  12. * Sets the value of synchronization state.
  13. * This operation has memory semantics of a {@code volatile} write.
  14. * @param newState the new state value
  15. */
  16. protected final void setState(int newState) {
  17. state = newState;
  18. }
  19. /**
  20. * Atomically sets synchronization state to the given updated
  21. * value if the current state value equals the expected value.
  22. * This operation has memory semantics of a {@code volatile} read
  23. * and write.
  24. *
  25. * @param expect the expected value
  26. * @param update the new value
  27. * @return {@code true} if successful. False return indicates that the actual
  28. * value was not equal to the expected value.
  29. */
  30. protected final boolean compareAndSetState(int expect, int update) {
  31. // See below for intrinsics setup to support this
  32. return unsafe.compareAndSwapInt(this, stateOffset, expect, update);
  33. }

Node

SHARED && EXCLUSIVE

  • 独占队列和共享队列是根据SHARED和EXCLUSIVE两个静态变量区分的

waitStatus

  • 0,默认初始值
  • CANCELLED,此节点由于超时或者中断被取消了,该状态不可以再次被修改为其他状态
  • SIGNAL
    • 表明后继节点在阻塞队列中,需要被唤醒,当前节点释放后(release)应该将后继节点从等待队列中移除(unpark)
  • CONDITION,表明此节点处于条件等待状态(条件等待队列中),在调用Condition的await相关方法后会进入条件队列,并等待其他线程唤醒。
  • PROPAGATE,传播

image.png

prev

  • 前驱节点

next

  • 后继节点

thread

  • 等待队列中的线程,对应当前节点

nextWaiter

  • 下一个等待节点,用于条件队列中

head

  • 头结点
  • 只有成功获取状态的节点才能成为头结点


tail

  • 尾结点


spinForTimeoutThreshold

  1. //设定的自旋时间
  2. static final long spinForTimeoutThreshold = 1000L;
  3. //在相关的抢夺锁过程中会有对超时时间的判断
  4. if (shouldParkAfterFailedAcquire(p, node) &&
  5. nanosTimeout > spinForTimeoutThreshold)
  6. LockSupport.parkNanos(this, nanosTimeout);

ConditionObject

  • 条件控制器,Condition接口实现类
  1. /**
  2. * Condition
  3. */
  4. public interface Condition {
  5. /**
  6. * 挂起
  7. */
  8. void await() throws InterruptedException;
  9. void awaitUninterruptibly();
  10. long awaitNanos(long nanosTimeout) throws InterruptedException;
  11. boolean await(long time, TimeUnit unit) throws InterruptedException;
  12. boolean awaitUntil(Date deadline) throws InterruptedException;
  13. /**
  14. * 唤醒
  15. */
  16. void signal();
  17. void signalAll();
  18. }
  19. /**
  20. * ConditionObject
  21. */
  22. public class ConditionObject implements Condition, java.io.Serializable {
  23. private static final long serialVersionUID = 1173984872572414699L;
  24. /** First node of condition queue. */
  25. private transient Node firstWaiter;
  26. /** Last node of condition queue. */
  27. private transient Node lastWaiter;
  28. /**
  29. * Creates a new {@code ConditionObject} instance.
  30. */
  31. public ConditionObject() { }
  32. ....
  33. }

一般通过Sync对象去构造一个ConditionObject对象,用于Condition的相关操作

  1. public Condition newCondition() {
  2. return sync.newCondition();
  3. }