虽然很少和AQS直接打交道,但是Java并发工具的底层基础,所以还是要了解队列同步器的运行机制

直接看源代码

AbstractQueuedSynchronizer 内部有两个内部类:NodeConditionObject,是aqs的基石。

  1. public abstract class AbstractQueuedSynchronizer extends AbstractOwnableSynchronizer {
  2. public class ConditionObject implements Condition {
  3. private transient Node firstWaiter;
  4. private transient Node lastWaiter;
  5. }
  6. static final class Node {
  7. static final Node SHARED = new Node();
  8. volatile int waitStatus;
  9. volatile Node prev;
  10. volatile Node next;
  11. volatile Thread thread;
  12. Node nextWaiter;
  13. Node() { // Used to establish initial head or SHARED marker
  14. }
  15. Node(Thread thread, Node mode) { // Used by addWaiter
  16. this.nextWaiter = mode;
  17. this.thread = thread;
  18. }
  19. Node(Thread thread, int waitStatus) { // Used by Condition
  20. this.waitStatus = waitStatus;
  21. this.thread = thread;
  22. }
  23. }
  24. }