Example usage for java.lang.management ThreadMXBean findMonitorDeadlockedThreads

List of usage examples for java.lang.management ThreadMXBean findMonitorDeadlockedThreads

Introduction

In this page you can find the example usage for java.lang.management ThreadMXBean findMonitorDeadlockedThreads.

Prototype

public long[] findMonitorDeadlockedThreads();

Source Link

Document

Finds cycles of threads that are in deadlock waiting to acquire object monitors.

Usage

From source file:Main.java

public static String getDeadlockedThreads() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] deadlocked = bean.findMonitorDeadlockedThreads();
    if (deadlocked == null || deadlocked.length == 0) {
        return "";
    }//from w  ww .  j  a v a 2 s.c o  m

    String buf = "Deadlocked thread IDs: ";
    for (long id : deadlocked) {
        buf += id + " ";
    }

    return buf;
}

From source file:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java

static String buildDeadlockInfo() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds != null && threadIds.length > 0) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter out = new PrintWriter(stringWriter);

        ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
        for (ThreadInfo ti : infos) {
            printThreadInfo(ti, out);//from w w  w  .j a  v  a 2  s  .c om
            printLockInfo(ti.getLockedSynchronizers(), out);
            out.println();
        }

        out.close();
        return stringWriter.toString();
    } else {
        return null;
    }
}

From source file:Main.java

public static String dumpThreads() {
    StringBuilder sb = new StringBuilder();
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] ids = bean.getAllThreadIds();
    _printThreads(bean, ids, sb);/*from w w  w. j  av  a2s. c o  m*/
    long[] deadlocks = bean.findDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }

    deadlocks = bean.findMonitorDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("monitor deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }
    return sb.toString();
}

From source file:EventDispatchThreadHangMonitor.java

private static void checkForDeadlock() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds == null) {
        return;//w w w .  ja  v  a 2 s  .  c  o  m
    }
    Log.warn("deadlock detected involving the following threads:");
    ThreadInfo[] threadInfos = threadBean.getThreadInfo(threadIds, Integer.MAX_VALUE);
    for (ThreadInfo info : threadInfos) {
        Log.warn("Thread #" + info.getThreadId() + " " + info.getThreadName() + " (" + info.getThreadState()
                + ") waiting on " + info.getLockName() + " held by " + info.getLockOwnerName()
                + stackTraceToString(info.getStackTrace()));
    }
}

From source file:net.bull.javamelody.internal.model.JavaInformations.java

private static long[] getDeadlockedThreads(ThreadMXBean threadBean) {
    final long[] deadlockedThreads;
    if (threadBean.isSynchronizerUsageSupported()) {
        deadlockedThreads = threadBean.findDeadlockedThreads();
    } else {/*w  w  w.ja  v a  2 s  .c o m*/
        deadlockedThreads = threadBean.findMonitorDeadlockedThreads();
    }
    if (deadlockedThreads != null) {
        Arrays.sort(deadlockedThreads);
    }
    return deadlockedThreads;
}

From source file:ca.simplegames.micro.controllers.StatsController.java

public void execute(MicroContext context, Map configuration) throws ControllerException {
    Map<String, Object> systemInfo = new HashMap<String, Object>();
    Map<String, Object> osMap = new HashMap<String, Object>();
    MBeanServerConnection mbeanServer = ManagementFactory.getPlatformMBeanServer();

    OperatingSystemMXBean sunOperatingSystemMXBean = null;
    try {/*from  ww w.  j  ava  2 s  .c o  m*/
        sunOperatingSystemMXBean = ManagementFactory.newPlatformMXBeanProxy(mbeanServer,
                ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);
    } catch (IOException e) {
        throw new ControllerException(e.getMessage());
    }

    Runtime rt = Runtime.getRuntime();
    long totalMemory = rt.totalMemory() / MEGA_BYTE;
    long freeMemory = rt.freeMemory() / MEGA_BYTE;
    long usedMemory = totalMemory - freeMemory;

    final long p100 = (int) Math.round(((double) freeMemory / (double) totalMemory) * 100);

    Map<String, Long> memInfo = new HashMap<String, Long>();

    memInfo.put("total", totalMemory);
    memInfo.put("used", usedMemory);
    memInfo.put("free", freeMemory);
    memInfo.put("percent_free", p100);

    systemInfo.put("memory", memInfo);
    systemInfo.put("powered_by", POWERED_BY_MICRO);

    //cpu usage in milli secs
    long currentCpuUsage = sunOperatingSystemMXBean.getProcessCpuTime() / 1000000;
    osMap.put("cpu_usage", currentCpuUsage);
    osMap.put("available_processors", sunOperatingSystemMXBean.getAvailableProcessors());
    osMap.put("system_load_average", sunOperatingSystemMXBean.getSystemLoadAverage());
    osMap.put("committed_virtual_memory_size", sunOperatingSystemMXBean.getCommittedVirtualMemorySize());
    osMap.put("free_physical_memory_size", sunOperatingSystemMXBean.getFreePhysicalMemorySize());
    osMap.put("total_physical_memory_size", sunOperatingSystemMXBean.getTotalPhysicalMemorySize());
    osMap.put("free_swap_space_size", sunOperatingSystemMXBean.getFreeSwapSpaceSize());
    osMap.put("total_swap_space_size", sunOperatingSystemMXBean.getTotalSwapSpaceSize());

    systemInfo.put("os", osMap);

    List<GarbageCollectorMXBean> gc = ManagementFactory.getGarbageCollectorMXBeans();
    List<Map> gcInfo = new ArrayList<Map>();

    for (GarbageCollectorMXBean aGc : gc) {
        Map<String, Object> gcMap = new HashMap<String, Object>();
        gcMap.put("name", aGc.getName());
        gcMap.put("collection_count", aGc.getCollectionCount());
        gcMap.put("collection_time", aGc.getCollectionTime());

        gcInfo.add(gcMap);
    }

    systemInfo.put("gc", gcInfo);

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    Map<String, Object> threadInfoMap = new HashMap<String, Object>(); // more to come ;)
    threadInfoMap.put("peak_thread_count", threadMXBean.getPeakThreadCount());
    threadInfoMap.put("thread_count", threadMXBean.getThreadCount());
    threadInfoMap.put("total_started_thread_count", threadMXBean.getTotalStartedThreadCount());

    long[] deadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
    threadInfoMap.put("dead_locked_thread_count", deadlockedThreads != null ? deadlockedThreads.length : 0);
    systemInfo.put("thread_info", threadInfoMap);

    JSONObject sysinfoJson = new JSONObject(Collections.singletonMap("system_info", systemInfo));

    String sysinfoString = null;
    try {
        sysinfoString = context.getRequest().getParameter("pretty") != null ? sysinfoJson.toString(2)
                : sysinfoJson.toString();
    } catch (JSONException e) {
        e.printStackTrace();
        throw new ControllerException(e.getMessage());
    }

    context.getRackResponse().withContentType(Mime.mimeType(JSON_TYPE)).withBody(sysinfoString)
            .withContentLength(sysinfoString.length()).with(Rack.MESSAGE_STATUS, HttpServletResponse.SC_OK);

    context.halt();
}

From source file:org.fluentd.jvmwatcher.proxy.JvmClientProxy.java

/**
 * @return/*  w  w  w. j ava 2 s . co m*/
 * @throws IOException
 */
public long[] findDeadlockedThreads() throws IOException {
    ThreadMXBean threadMx = getThreadMXBean();
    if (this.supportsLockUsage_ && threadMx.isSynchronizerUsageSupported()) {
        return threadMx.findDeadlockedThreads();
    } else {
        return threadMx.findMonitorDeadlockedThreads();
    }
}

From source file:Main.java

public static String takeDumpJSON(ThreadMXBean threadMXBean) throws IOException {
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    List<Map<String, Object>> threads = new ArrayList<>();

    for (ThreadInfo thread : threadInfos) {
        Map<String, Object> threadMap = new HashMap<>();
        threadMap.put("name", thread.getThreadName());
        threadMap.put("id", thread.getThreadId());
        threadMap.put("state", thread.getThreadState().name());
        List<String> stacktrace = new ArrayList<>();
        for (StackTraceElement element : thread.getStackTrace()) {
            stacktrace.add(element.toString());
        }//from ww  w .j a va  2s .c  om
        threadMap.put("stack", stacktrace);

        if (thread.getLockName() != null) {
            threadMap.put("lock_name", thread.getLockName());
        }
        if (thread.getLockOwnerId() != -1) {
            threadMap.put("lock_owner_id", thread.getLockOwnerId());
        }
        if (thread.getBlockedTime() > 0) {
            threadMap.put("blocked_time", thread.getBlockedTime());
        }
        if (thread.getBlockedCount() > 0) {
            threadMap.put("blocked_count", thread.getBlockedCount());
        }
        if (thread.getLockedMonitors().length > 0) {
            threadMap.put("locked_monitors", thread.getLockedMonitors());
        }
        if (thread.getLockedSynchronizers().length > 0) {
            threadMap.put("locked_synchronizers", thread.getLockedSynchronizers());
        }
        threads.add(threadMap);
    }
    ObjectMapper om = new ObjectMapper();
    ObjectNode json = om.createObjectNode();
    json.put("date", new Date().toString());
    json.putPOJO("threads", threads);

    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
    long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        json.putPOJO("deadlocked_thread_ids", deadlockedThreads);
    }
    if (monitorDeadlockedThreads != null && monitorDeadlockedThreads.length > 0) {
        json.putPOJO("monitor_deadlocked_thread_ids", monitorDeadlockedThreads);
    }
    om.enable(SerializationFeature.INDENT_OUTPUT);
    return om.writerWithDefaultPrettyPrinter().writeValueAsString(json);
}