Skip to content

Commit fd5d799

Browse files
committed
Polish
See gh-5222
1 parent a556022 commit fd5d799

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/jvm/JvmThreadDeadlockMetrics.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,12 @@
3131
import static java.util.Collections.emptyList;
3232

3333
/**
34-
* {@link MeterBinder} for JVM deadlocked threads. These metrics are somewhat expensive to
35-
* collect, so they should not be enabled unless necessary. To enable these metrics, add
36-
* the following to your application's code ({@code registry} is the meter registry being
37-
* used by your application):
38-
* <pre>{@code new JvmDeadlockedThreadMetrics().bindTo(registry);}</pre>
34+
* {@link MeterBinder} for JVM deadlocked threads. These metrics may be expensive to
35+
* collect. Consider that when deciding whether to enable these metrics. To enable these
36+
* metrics, bind an instance of this to a {@link MeterRegistry}.
3937
*
4038
* @author Ruth Kurniawati
39+
* @since 1.14.0
4140
*/
4241
@NonNullApi
4342
@NonNullFields
@@ -59,7 +58,7 @@ public JvmThreadDeadlockMetrics(Iterable<Tag> tags) {
5958
public void bindTo(MeterRegistry registry) {
6059
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
6160

62-
if (threadBean.isObjectMonitorUsageSupported()) {
61+
if (threadBean.isSynchronizerUsageSupported()) {
6362
Gauge.builder("jvm.threads.deadlocked", threadBean, JvmThreadDeadlockMetrics::getDeadlockedThreadCount)
6463
.tags(tags)
6564
.description("The current number of threads that are deadlocked")

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/jvm/JvmThreadDeadlockMetricsTest.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,36 +40,10 @@
4040
*/
4141
class JvmThreadDeadlockMetricsTest {
4242

43-
private static class DeadlockedThread {
44-
45-
private final Thread thread;
46-
47-
DeadlockedThread(CountDownLatch lock1IsLocked, Lock lock1, CountDownLatch lock2IsLocked, Lock lock2) {
48-
this.thread = new Thread(() -> {
49-
try {
50-
lock1.lock();
51-
lock1IsLocked.countDown();
52-
lock2IsLocked.await();
53-
lock2.lockInterruptibly();
54-
}
55-
catch (InterruptedException ignored) {
56-
}
57-
});
58-
}
59-
60-
void start() {
61-
thread.start();
62-
}
63-
64-
void interrupt() {
65-
thread.interrupt();
66-
}
67-
68-
}
43+
MeterRegistry registry = new SimpleMeterRegistry();
6944

7045
@Test
7146
void deadlockedThreadMetrics() {
72-
MeterRegistry registry = new SimpleMeterRegistry();
7347
new JvmThreadDeadlockMetrics().bindTo(registry);
7448
final CountDownLatch lock1IsLocked = new CountDownLatch(1);
7549
final CountDownLatch lock2IsLocked = new CountDownLatch(1);
@@ -90,16 +64,14 @@ void deadlockedThreadMetrics() {
9064
}
9165

9266
@Test
93-
void whenJvmDoesntSupportObjectMonitorUsageJvmThreadsDeadlockedMetricShouldNotBeRegistered() {
94-
MeterRegistry registry = new SimpleMeterRegistry();
67+
void whenJvmDoesntSupportSynchronizerUsage_JvmThreadsDeadlockedMetricShouldNotBeRegistered() {
9568
try (MockedStatic<ManagementFactory> mockedStatic = Mockito.mockStatic(ManagementFactory.class)) {
9669
ThreadMXBean threadBean = mock(ThreadMXBean.class);
97-
when(threadBean.isObjectMonitorUsageSupported()).thenReturn(false);
70+
when(threadBean.isSynchronizerUsageSupported()).thenReturn(false);
9871
mockedStatic.when(ManagementFactory::getThreadMXBean).thenReturn(threadBean);
9972
new JvmThreadDeadlockMetrics().bindTo(registry);
10073

101-
// ObjectMonitorUsage is not supported, so this metric should not be
102-
// registered
74+
// synchronizer usage is not monitored, so this should not be registered
10375
assertThat(registry.find("jvm.threads.deadlocked").gauge()).isNull();
10476
// but this one is still supported
10577
assertThat(registry.find("jvm.threads.deadlocked.monitor").gauge()).isNotNull();
@@ -120,4 +92,31 @@ void getDeadlockedThreadCountWhenFindMonitorDeadlockedThreadsIsNullShouldWork()
12092
assertThat(JvmThreadDeadlockMetrics.getDeadlockedMonitorThreadCount(threadBean)).isEqualTo(0);
12193
}
12294

95+
private static class DeadlockedThread {
96+
97+
private final Thread thread;
98+
99+
DeadlockedThread(CountDownLatch lock1IsLocked, Lock lock1, CountDownLatch lock2IsLocked, Lock lock2) {
100+
this.thread = new Thread(() -> {
101+
try {
102+
lock1.lock();
103+
lock1IsLocked.countDown();
104+
lock2IsLocked.await();
105+
lock2.lockInterruptibly();
106+
}
107+
catch (InterruptedException ignored) {
108+
}
109+
});
110+
}
111+
112+
void start() {
113+
thread.start();
114+
}
115+
116+
void interrupt() {
117+
thread.interrupt();
118+
}
119+
120+
}
121+
123122
}

0 commit comments

Comments
 (0)