Concurrency Coordination Reports
Lock State Coordination Report
Acquire a reentrant lock several times and report its nested hold count.
Lock State Coordination Report
LockStateCoordinationReport.java
import java.util.concurrent.locks.ReentrantLock;
public class LockStateCoordinationReport {
public static void main(String[] args) {
int reentries = ;
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 = ;
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 = ;
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);
}
}
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.