我们知道 对象存储在 eden区,当eden区满了之后会触发 minor gc,eden和 from 存活的对象使用 copy 复制到 to中,存活的对象年龄加1然后和 from进行交换,当对象年龄达到15(在from 和 to之间来回交换15次),就会将对象复制到老年代中,老年代如果内存不够才会触发 full gc。
/** * Performs {@link Lock#lock}. The main reason for subclassing * is to allow fast path for nonfair version. */ abstractvoidlock(); //ReentrantLock.lock()方法实际上是Sync子类的方法(分公平锁和非公平锁) } //继承于 Sync的非公平锁 staticfinalclassNonfairSyncextendsSync { privatestaticfinallongserialVersionUID=7316153563782823691L;
/** * Fair version of tryAcquire. Don't grant access unless * recursive call or no waiters or is first. */ protectedfinalbooleantryAcquire(int acquires) { finalThreadcurrent= Thread.currentThread(); //获取当前线程 intc= getState(); //得到volatile修饰的state变量的值,默认是0 if (c == 0) { //如果是0,说明当前锁没有被占用 if (!hasQueuedPredecessors() && compareAndSetState(0, acquires)) { setExclusiveOwnerThread(current); returntrue; } } elseif (current == getExclusiveOwnerThread()) { intnextc= c + acquires; if (nextc < 0) thrownewError("Maximum lock count exceeded"); setState(nextc); returntrue; } returnfalse; } //注意!这个方法也是AQS类的,用来检查当前线程是否是队列中的第一个线程 publicfinalbooleanhasQueuedPredecessors() { // The correctness of this depends on head being initialized // before tail and on head.next being accurate if the current // thread is first in queue. Nodet= tail; // Read fields in reverse initialization order Nodeh= head; Node s; return h != t && ((s = h.next) == null || s.thread != Thread.currentThread()); } } }
//AQS抽象类 publicabstractclassAbstractQueuedSynchronizer extendsAbstractOwnableSynchronizer implementsjava.io.Serializable { /** * Head of the wait queue, lazily initialized. Except for * initialization, it is modified only via method setHead. Note: * If head exists, its waitStatus is guaranteed not to be * CANCELLED. */ privatetransientvolatile Node head; //队列首部
/** * Tail of the wait queue, lazily initialized. Modified only via * method enq to add new wait node. */ privatetransientvolatile Node tail; //队列尾部 /** * The current owner of exclusive mode synchronization. */ privatetransient Thread exclusiveOwnerThread; //持有当前锁的线程
/** * Head of the wait queue, lazily initialized. Except for * initialization, it is modified only via method setHead. Note: * If head exists, its waitStatus is guaranteed not to be * CANCELLED. */ privatetransientvolatile Node head; //队列首部
/** * Tail of the wait queue, lazily initialized. Modified only via * method enq to add new wait node. */ privatetransientvolatile Node tail; //队列尾部
/** * The synchronization state. */ privatevolatileint state; //锁状态,加锁成功则为1,重入+1,释放-1
/** * The current owner of exclusive mode synchronization. */ privatetransient Thread exclusiveOwnerThread; //持有当前锁的线程
//判断是否轮到当前线程上锁 publicfinalbooleanhasQueuedPredecessors() { // The correctness of this depends on head being initialized // before tail and on head.next being accurate if the current // thread is first in queue. Nodet= tail; // Read fields in reverse initialization order Nodeh= head; Node s; return h != t && ((s = h.next) == null || s.thread != Thread.currentThread()); }
finalbooleanacquireQueued(final Node node, int arg) { booleanfailed=true; try { booleaninterrupted=false; //注意死循环,会执行多次 for (;;) { finalNodep= node.predecessor(); //获取当前结点的前一个结点 if (p == head && tryAcquire(arg)) { //tryAcquire(arg)进行自旋操作,前一个为头结点(null) setHead(node); //自旋成功,拿到锁的资源后将当前结点设置为头结点 p.next = null; //将之前头结点断开,便于进行GC回收 failed = false; return interrupted; } if (shouldParkAfterFailedAcquire(p, node) && //自旋失败就需要判断是否应该释放cpu资源 parkAndCheckInterrupt()) interrupted = true; } } finally { if (failed) cancelAcquire(node); } }
//判断自旋失败是否需要释放cpu资源 privatestaticbooleanshouldParkAfterFailedAcquire(Node pred, Node node) { intws= pred.waitStatus; //获取到前一个结点的状态(注意是前一个!!!,默认是0) if (ws == Node.SIGNAL) //Node.SIGNAL = -1 /* * This node has already set status asking a release * to signal it, so it can safely park. */ returntrue; if (ws > 0) { /* * Predecessor was cancelled. Skip over predecessors and * indicate retry. */ do { node.prev = pred = pred.prev; } while (pred.waitStatus > 0); pred.next = node; } else { /* * waitStatus must be 0 or PROPAGATE. Indicate that we * need a signal, but don't park yet. Caller will need to * retry to make sure it cannot acquire before parking. */ compareAndSetWaitStatus(pred, ws, Node.SIGNAL); //CAS原子操作设置状态为-1 } returnfalse; }