Last active
May 25, 2021 07:55
-
-
Save geminicaprograms/a8b8e0bfe954782866396ee11c7c3967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.management.ManagementFactory; | |
import java.lang.management.ThreadInfo; | |
import java.lang.management.ThreadMXBean; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Collections; | |
import java.util.Iterator; | |
import java.util.List; | |
import java.util.Random; | |
import java.util.concurrent.atomic.AtomicInteger; | |
public class Threads { | |
private static final Object lockObject = new Object(); | |
private static final Random rand = new Random(); | |
private static final AtomicInteger finishedThreads = new AtomicInteger(0); | |
private static final List<Long> results = new ArrayList<>(200); | |
private static class BlockedThreadsMetric extends Thread { | |
@Override | |
public void run() { | |
while (finishedThreads.get() < 200) { | |
try { | |
Thread.sleep(200L); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
long startTime = System.currentTimeMillis(); | |
long blocked = 0L; | |
ThreadInfo[] threads = null; | |
try { | |
ThreadMXBean threadsBean = ManagementFactory.getThreadMXBean(); | |
threads = | |
threadsBean.dumpAllThreads( | |
threadsBean.isObjectMonitorUsageSupported(), | |
threadsBean.isSynchronizerUsageSupported()); | |
if (threads == null) { | |
System.out.println("There are no threads started"); | |
return; | |
} | |
blocked = | |
Arrays.stream(threads) | |
.filter( | |
t -> t.getLockInfo() != null && Thread.State.BLOCKED == t.getThreadState()) | |
.count(); | |
// measure deadlocks finding | |
// long[] ids = | |
// threadsBean.isSynchronizerUsageSupported() | |
// ? threadsBean.findDeadlockedThreads() | |
// : threadsBean.findDeadlockedThreads(); | |
// blocked = ids != null ? ids.length : 0L; | |
// | |
// optimised thread dump that doesn't look up for stach trace | |
// threads = threadsBean.getThreadInfo(threadsBean.getAllThreadIds(), 0); | |
} finally { | |
long measurement = System.currentTimeMillis() - startTime; | |
results.add(measurement); | |
System.out.println( | |
String.format( | |
"Threads %d, blocked threads %d, measurement took: %s ms", | |
threads != null ? threads.length : 0, blocked, measurement)); | |
} | |
} | |
System.out.println(String.format("Total results: %d", results.size())); | |
Collections.sort(results); | |
System.out.println(String.format("min: %d", results.get(0))); | |
System.out.println(String.format("50: %d", percentile(results, 50))); | |
System.out.println(String.format("90: %d", percentile(results, 90))); | |
System.out.println(String.format("95: %d", percentile(results, 95))); | |
System.out.println(String.format("99: %d", percentile(results, 99))); | |
System.out.println(String.format("max: %d", results.get(results.size() - 1))); | |
} | |
} | |
private static class WorkThread extends Thread { | |
@Override | |
public void run() { | |
try { | |
synchronized (lockObject) { | |
Thread.sleep(finishedThreads.get() < 250 ? (rand.nextInt(200) + 100) : 0); | |
finishedThreads.incrementAndGet(); | |
} | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} finally { | |
} | |
} | |
} | |
public static void main(String[] args) { | |
int totalThreads = args.length == 1 ? Integer.valueOf(args[0]) : 1000; | |
BlockedThreadsMetric m = new BlockedThreadsMetric(); | |
m.setName("M"); | |
m.start(); | |
for (int i = 0; i < totalThreads; i++) { | |
WorkThread t = new WorkThread(); | |
t.setName(String.format("W-%d", i)); | |
t.start(); | |
} | |
System.out.println(String.format("Started all %d threads", totalThreads)); | |
} | |
private static long percentile(List<Long> latencies, double percentile) { | |
int index = (int) Math.ceil(percentile / 100.0 * latencies.size()); | |
return latencies.get(index - 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Once it is compiled one calls it with
java -cp ./bin Threads XXXX
where XXXX is the number of threads to be started (by default if no argument given 1000 threads are started).Here are some stats (in
millis
) that I have generated on my laptop for getting blocked threads depending on the number of threads and Java version (each Java version runs against1000
and4000
threads correspondingly).For comparison the same stats for the existing deadlocks finding (code is commented in the above snippet)
And finally the optimised version of thread dump that doesn't exercise the stack trace (also in commented part of the snipped)
And here is the chart comparison (left 5 chart bars for each category are results of
deadlocked
, middle 5 are results ofblocked v1
and right 5 are forblocked v2
threads call):