Concurrency Coordination Reports
Lock State Coordination Report
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
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);
}
}
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;for (int i = 0; i < 1 + reentries; i++)
pass 1 of 29for (int i0 = 0; i < 1 + reentries1; i++) {10 if (lock.tryLock()) {acquired ← 1
pass 1 of 29for (int i = 0; i < 1 + reentries; i++) {10 if (lock.tryLock()) {11 acquired→ 1++;12 }for (int i = 0; i < 1 + reentries; i++)
pass 2 of 29for (int i1 = 0; i < 1 + reentries1; i++) {10 if (lock.tryLock()) {acquired ← 2
pass 2 of 29for (int i = 0; i < 1 + reentries; i++) {10 if (lock.tryLock()) {11 acquired→ 2++;12 }hold ← 2, status ← nested
15int hold→ 2 = lock.getHoldCount();16String status→ nested = hold2 <= 1 ? "held" : hold == 2 ? "nested" : "deep";for (int i = 0; i < acquired; i++)
pass 1 of 218for (int i0 = 0; i < acquired2; i++) {19 lock.unlock();20}for (int i = 0; i < acquired; i++)
pass 2 of 218for (int i1 = 0; i < acquired2; i++) {19 lock.unlock();20}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
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;for (int i = 0; i < 1 + reentries; i++)
9for (int i0 = 0; i < 1 + reentries0; i++) {10 if (lock.tryLock()) {acquired ← 1
9for (int i = 0; i < 1 + reentries; i++) {10 if (lock.tryLock()) {11 acquired→ 1++;12 }hold ← 1, status ← held
15int hold→ 1 = lock.getHoldCount();16String status→ held = hold1 <= 1 ? "held" : hold == 2 ? "nested" : "deep";for (int i = 0; i < acquired; i++)
18for (int i0 = 0; i < acquired1; i++) {19 lock.unlock();20}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
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;for (int i = 0; i < 1 + reentries; i++)
pass 1 of 39for (int i0 = 0; i < 1 + reentries2; i++) {10 if (lock.tryLock()) {All 3 passes — pass 1 is the card above pass i1 0 2 1 3 2 acquired ← 1
pass 1 of 39for (int i = 0; i < 1 + reentries; i++) {10 if (lock.tryLock()) {11 acquired→ 1++;12 }All 3 passes — pass 1 is the card above pass acquired1 0 → 1 2 1 → 2 3 2 → 3 hold ← 3, status ← deep
15int hold→ 3 = lock.getHoldCount();16String status→ deep = hold3 <= 1 ? "held" : hold == 2 ? "nested" : "deep";for (int i = 0; i < acquired; i++)
pass 1 of 318for (int i0 = 0; i < acquired3; i++) {19 lock.unlock();20}All 3 passes — pass 1 is the card above pass i1 0 2 1 3 2 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