Acquire a reentrant lock several times and report its nested hold count.

reentrant lock A `ReentrantLock` lets the same thread acquire it more than once and tracks a hold count, so the report shows how deeply the lock is nested.

Lock State Coordination Report

reentries
LockStateCoordinationReport.java
Replay: real traced execution (multi-file project)
import java.util.concurrent.locks.ReentrantLock;

public class LockStateCoordinationReport {
    public static void main(String[] args) {
        int reentries = 1;
        ReentrantLock lock = new ReentrantLock();
        int acquired = 0;

        for (int i = 0; i < 1 + reentries; i++) {
            if (lock.tryLock()) {
                acquired++;
            }
        }

        int hold = lock.getHoldCount();
        String status = hold <= 1 ? "held" : hold == 2 ? "nested" : "deep";

        for (int i = 0; i < acquired; i++) {
            lock.unlock();
        }

        System.out.println("reentries=" + reentries + " acquired=" + acquired + " hold=" + hold + " status=" + status);
    }
}
import java.util.concurrent.locks.ReentrantLock;

public class LockStateCoordinationReport {
    public static void main(String[] args) {
        int reentries = 0;
        ReentrantLock lock = new ReentrantLock();
        int acquired = 0;

        for (int i = 0; i < 1 + reentries; i++) {
            if (lock.tryLock()) {
                acquired++;
            }
        }

        int hold = lock.getHoldCount();
        String status = hold <= 1 ? "held" : hold == 2 ? "nested" : "deep";

        for (int i = 0; i < acquired; i++) {
            lock.unlock();
        }

        System.out.println("reentries=" + reentries + " acquired=" + acquired + " hold=" + hold + " status=" + status);
    }
}
import java.util.concurrent.locks.ReentrantLock;

public class LockStateCoordinationReport {
    public static void main(String[] args) {
        int reentries = 2;
        ReentrantLock lock = new ReentrantLock();
        int acquired = 0;

        for (int i = 0; i < 1 + reentries; i++) {
            if (lock.tryLock()) {
                acquired++;
            }
        }

        int hold = lock.getHoldCount();
        String status = hold <= 1 ? "held" : hold == 2 ? "nested" : "deep";

        for (int i = 0; i < acquired; i++) {
            lock.unlock();
        }

        System.out.println("reentries=" + reentries + " acquired=" + acquired + " hold=" + hold + " status=" + status);
    }
}
  1. reentries ← 1, lock ← ⟨ReentrantLock A⟩[Unlocked], acquired ← 0

    3public class LockStateCoordinationReport {4    public static void main(String[] args) {5        int reentries→ 1 = 1;  //@reentries=0, 26        ReentrantLock lock→ ⟨ReentrantLock A⟩[Unlocked] = new ReentrantLock();7        int acquired→ 0 = 0;
  2. for (int i = 0; i < 1 + reentries; i++)

    pass 1 of 2
    9for (int i0 = 0; i < 1 + reentries1; i++) {10    if (lock.tryLock()) {
  3. acquired ← 1

    pass 1 of 2
    9for (int i = 0; i < 1 + reentries; i++) {10    if (lock.tryLock()) {11        acquired→ 1++;12    }
  4. for (int i = 0; i < 1 + reentries; i++)

    pass 2 of 2
    9for (int i1 = 0; i < 1 + reentries1; i++) {10    if (lock.tryLock()) {
  5. acquired ← 2

    pass 2 of 2
    9for (int i = 0; i < 1 + reentries; i++) {10    if (lock.tryLock()) {11        acquired→ 2++;12    }
  6. hold ← 2, status ← nested

    15int hold→ 2 = lock.getHoldCount();16String status→ nested = hold2 <= 1 ? "held" : hold == 2 ? "nested" : "deep";
  7. for (int i = 0; i < acquired; i++)

    pass 1 of 2
    18for (int i0 = 0; i < acquired2; i++) {19    lock.unlock();20}
  8. for (int i = 0; i < acquired; i++)

    pass 2 of 2
    18for (int i1 = 0; i < acquired2; i++) {19    lock.unlock();20}
  9. System.out.println("reentries=" + reentries + " acquired=" + acquired …

    22    System.out.println("reentries=" + reentries1 + " acquired=" + acquired2 + " hold=" + hold2 + " status=" + statusnested);23}
    outputreentries=1 acquired=2 hold=2 status=nested
  1. reentries ← 0, lock ← ⟨ReentrantLock A⟩[Unlocked], acquired ← 0

    3public class LockStateCoordinationReport {4    public static void main(String[] args) {5        int reentries→ 0 = 0;6        ReentrantLock lock→ ⟨ReentrantLock A⟩[Unlocked] = new ReentrantLock();7        int acquired→ 0 = 0;
  2. for (int i = 0; i < 1 + reentries; i++)

    9for (int i0 = 0; i < 1 + reentries0; i++) {10    if (lock.tryLock()) {
  3. acquired ← 1

    9for (int i = 0; i < 1 + reentries; i++) {10    if (lock.tryLock()) {11        acquired→ 1++;12    }
  4. hold ← 1, status ← held

    15int hold→ 1 = lock.getHoldCount();16String status→ held = hold1 <= 1 ? "held" : hold == 2 ? "nested" : "deep";
  5. for (int i = 0; i < acquired; i++)

    18for (int i0 = 0; i < acquired1; i++) {19    lock.unlock();20}
  6. System.out.println("reentries=" + reentries + " acquired=" + acquired …

    22    System.out.println("reentries=" + reentries0 + " acquired=" + acquired1 + " hold=" + hold1 + " status=" + statusheld);23}
    outputreentries=0 acquired=1 hold=1 status=held
  1. reentries ← 2, lock ← ⟨ReentrantLock A⟩[Unlocked], acquired ← 0

    3public class LockStateCoordinationReport {4    public static void main(String[] args) {5        int reentries→ 2 = 2;6        ReentrantLock lock→ ⟨ReentrantLock A⟩[Unlocked] = new ReentrantLock();7        int acquired→ 0 = 0;
  2. for (int i = 0; i < 1 + reentries; i++)

    pass 1 of 3
    9for (int i0 = 0; i < 1 + reentries2; i++) {10    if (lock.tryLock()) {
    All 3 passes — pass 1 is the card above
    passi
    10
    21
    32
  3. acquired ← 1

    pass 1 of 3
    9for (int i = 0; i < 1 + reentries; i++) {10    if (lock.tryLock()) {11        acquired→ 1++;12    }
    All 3 passes — pass 1 is the card above
    passacquired
    10 1
    21 2
    32 3
  4. hold ← 3, status ← deep

    15int hold→ 3 = lock.getHoldCount();16String status→ deep = hold3 <= 1 ? "held" : hold == 2 ? "nested" : "deep";
  5. for (int i = 0; i < acquired; i++)

    pass 1 of 3
    18for (int i0 = 0; i < acquired3; i++) {19    lock.unlock();20}
    All 3 passes — pass 1 is the card above
    passi
    10
    21
    32
  6. System.out.println("reentries=" + reentries + " acquired=" + acquired …

    22    System.out.println("reentries=" + reentries2 + " acquired=" + acquired3 + " hold=" + hold3 + " status=" + statusdeep);23}
    outputreentries=2 acquired=3 hold=3 status=deep