Package java.lang.management

Examples of java.lang.management.ThreadInfo


        StringBuilder sb=new StringBuilder();
        ThreadMXBean bean=ManagementFactory.getThreadMXBean();
        long[] ids=bean.getAllThreadIds();
        ThreadInfo[] threads=bean.getThreadInfo(ids, 20);
        for(int i=0; i < threads.length; i++) {
            ThreadInfo info=threads[i];
            if(info == null)
                continue;
            sb.append(info.getThreadName()).append(":\n");
            StackTraceElement[] stack_trace=info.getStackTrace();
            for(int j=0; j < stack_trace.length; j++) {
                StackTraceElement el=stack_trace[j];
                sb.append("at ").append(el.getClassName()).append(".").append(el.getMethodName());
                sb.append("(").append(el.getFileName()).append(":").append(el.getLineNumber()).append(")");
                sb.append("\n");
View Full Code Here


    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid: threadIds) {
      ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
      if (info == null) {
        stream.println("  Inactive");
        continue;
      }
      stream.println("Thread " +
                     getTaskName(info.getThreadId(),
                                 info.getThreadName()) + ":");
      Thread.State state = info.getThreadState();
      stream.println("  State: " + state);
      stream.println("  Blocked count: " + info.getBlockedCount());
      stream.println("  Waited count: " + info.getWaitedCount());
      if (contention) {
        stream.println("  Blocked time: " + info.getBlockedTime());
        stream.println("  Waited time: " + info.getWaitedTime());
      }
      if (state == Thread.State.WAITING) {
        stream.println("  Waiting on " + info.getLockName());
      } else  if (state == Thread.State.BLOCKED) {
        stream.println("  Blocked on " + info.getLockName());
        stream.println("  Blocked by " +
                       getTaskName(info.getLockOwnerId(),
                                   info.getLockOwnerName()));
      }
      stream.println("  Stack:");
      for (StackTraceElement frame: info.getStackTrace()) {
        stream.println("    " + frame.toString());
      }
    }
    stream.flush();
  }
View Full Code Here

                    if (threadMxBean == null) {
                        threadMxBean = ManagementFactory.getThreadMXBean();
                    }
                }
            }
            ThreadInfo threadInfo =
                    threadMxBean.getThreadInfo(logRecordThreadId);
            if (threadInfo == null) {
                return Long.toString(logRecordThreadId);
            }
            result = threadInfo.getThreadName();
        }

        cache.put(Integer.valueOf(logRecordThreadId), result);

        return result;
View Full Code Here

    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid: threadIds) {
      ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
      if (info == null) {
        stream.println("  Inactive");
        continue;
      }
      stream.println("Thread " +
                     getTaskName(info.getThreadId(),
                                 info.getThreadName()) + ":");
      Thread.State state = info.getThreadState();
      stream.println("  State: " + state);
      stream.println("  Blocked count: " + info.getBlockedCount());
      stream.println("  Waited count: " + info.getWaitedCount());
      if (contention) {
        stream.println("  Blocked time: " + info.getBlockedTime());
        stream.println("  Waited time: " + info.getWaitedTime());
      }
      if (state == Thread.State.WAITING) {
        stream.println("  Waiting on " + info.getLockName());
      } else  if (state == Thread.State.BLOCKED) {
        stream.println("  Blocked on " + info.getLockName());
        stream.println("  Blocked by " +
                       getTaskName(info.getLockOwnerId(),
                                   info.getLockOwnerName()));
      }
      stream.println("  Stack:");
      for (StackTraceElement frame: info.getStackTrace()) {
        stream.println("    " + frame.toString());
      }
    }
    stream.flush();
  }
View Full Code Here

  /**
   * @return true if the given thread ID appears to be a Jetty selector thread
   * based on its stack trace
   */
  private static boolean isJettySelectorThread(long tid) {
    ThreadInfo info = threadBean.getThreadInfo(tid, 20);
    for (StackTraceElement stack : info.getStackTrace()) {
      // compare class names instead of classses, since
      // jetty uses a different classloader
      if (SelectChannelConnector.class.getName().equals(
          stack.getClassName())) {
        LOG.debug("Thread #" + tid + " (" + info.getThreadName() + ") " +
            "is a Jetty selector thread.");
        return true;
      }
    }
    LOG.debug("Thread #" + tid + " (" + info.getThreadName() + ") " +
      "is not a jetty thread");
    return false;
  }
View Full Code Here

      assertEquals(ManagementFactory.THREAD_MXBEAN_NAME, mo.getName());
      validateComponentType(mo);
     
      ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
      long threadID = Thread.currentThread().getId();
      ThreadInfo threadInfo = mbean.getThreadInfo(threadID, 3);

      // Test ThreadInfo MetaValue wrap/unwrap
      MetaValueFactory metaValueFactory = MetaValueFactory.getInstance();
      CompositeDataMetaValueBuilder builder = new CompositeDataMetaValueBuilder();
      builder.setMetaValueFactory(metaValueFactory);
      metaValueFactory.setBuilder(CompositeData.class, builder);
      metaValueFactory.setBuilder(CompositeDataSupport.class, builder);

      MetaValue threadInfoMV = metaValueFactory.create(threadInfo);
      log.debug("ThreadInfo.MV: "+threadInfoMV);
      assertTrue(threadInfoMV instanceof CompositeValue);
      CompositeValue tiCV = CompositeValue.class.cast(threadInfoMV);
      ThreadInfo threadInfo2 = ManagementFactoryUtils.unwrapThreadInfo(tiCV);
      assertEquals(threadInfo.getLockOwnerName(), threadInfo2.getLockOwnerName());
      assertEquals(threadInfo.getThreadName(), threadInfo2.getThreadName());
      assertEquals(threadInfo.isInNative(), threadInfo2.isInNative());
      assertEquals(threadInfo.getBlockedCount(), threadInfo2.getBlockedCount());
      assertEquals(threadInfo.getBlockedTime(), threadInfo2.getBlockedTime());
      assertEquals(threadInfo.getLockOwnerId(), threadInfo2.getLockOwnerId());
      assertEquals(threadInfo.getThreadId(), threadInfo2.getThreadId());
      assertEquals(threadInfo.getThreadState(), threadInfo2.getThreadState());
      assertEquals(threadInfo.getWaitedCount(), threadInfo2.getWaitedCount());
      assertEquals(threadInfo.getWaitedTime(), threadInfo2.getWaitedTime());
      StackTraceElement[] st = threadInfo.getStackTrace();
      StackTraceElement[] st2 = threadInfo2.getStackTrace();
      for(int n = 0; n < st.length; n ++)
      {
         assertEquals(st[n], st2[n]);
      }

      // Properties
      ManagedProperty allThreadIds = mo.getProperty("allThreadIds");
      assertNotNull(allThreadIds);
      ManagedProperty currentThreadCpuTime = mo.getProperty("currentThreadCpuTime");
      assertNotNull(currentThreadCpuTime);
      long x = (Long) metaValueFactory.unwrap(currentThreadCpuTime.getValue());
      assertTrue(x > 1000);
      ManagedProperty currentThreadUserTime = mo.getProperty("currentThreadUserTime");
      assertNotNull(currentThreadUserTime);
      ManagedProperty daemonThreadCount = mo.getProperty("daemonThreadCount");
      assertNotNull(daemonThreadCount);
      x = (Integer) metaValueFactory.unwrap(daemonThreadCount.getValue());
      assertEquals(mbean.getDaemonThreadCount(), x);
      ManagedProperty peakThreadCount = mo.getProperty("peakThreadCount");
      assertNotNull(peakThreadCount);
      x = (Integer) metaValueFactory.unwrap(peakThreadCount.getValue());
      assertEquals(mbean.getPeakThreadCount(), x);
      ManagedProperty threadCount = mo.getProperty("threadCount");
      assertNotNull(threadCount);
      x = (Integer) metaValueFactory.unwrap(threadCount.getValue());
      assertEquals(mbean.getThreadCount(), x);
      ManagedProperty totalStartedThreadCount = mo.getProperty("totalStartedThreadCount");
      assertNotNull(totalStartedThreadCount);
      x = (Long) metaValueFactory.unwrap(totalStartedThreadCount.getValue());
      assertEquals(mbean.getTotalStartedThreadCount(), x);
      ManagedProperty currentThreadCpuTimeSupported = mo.getProperty("currentThreadCpuTimeSupported");
      assertNotNull(currentThreadCpuTimeSupported);
      boolean flag = (Boolean) metaValueFactory.unwrap(currentThreadCpuTimeSupported.getValue());
      assertEquals(mbean.isCurrentThreadCpuTimeSupported(), flag);
      ManagedProperty threadContentionMonitoringEnabled = mo.getProperty("threadContentionMonitoringEnabled");
      assertNotNull(threadContentionMonitoringEnabled);
      flag = (Boolean) metaValueFactory.unwrap(threadContentionMonitoringEnabled.getValue());
      assertEquals(mbean.isThreadContentionMonitoringEnabled(), flag);
      ManagedProperty threadContentionMonitoringSupported = mo.getProperty("threadContentionMonitoringSupported");
      assertNotNull(threadContentionMonitoringSupported);
      flag = (Boolean) metaValueFactory.unwrap(threadContentionMonitoringSupported.getValue());
      assertEquals(mbean.isThreadContentionMonitoringSupported(), flag);
      ManagedProperty threadCpuTimeEnabled = mo.getProperty("threadCpuTimeEnabled");
      assertNotNull(threadCpuTimeEnabled);
      flag = (Boolean) metaValueFactory.unwrap(threadCpuTimeEnabled.getValue());
      assertEquals(mbean.isThreadCpuTimeEnabled(), flag);
      ManagedProperty threadCpuTimeSupported = mo.getProperty("threadCpuTimeSupported");
      assertNotNull(threadCpuTimeSupported);
      flag = (Boolean) metaValueFactory.unwrap(threadCpuTimeSupported.getValue());
      assertEquals(mbean.isThreadCpuTimeSupported(), flag);

      // Ops
      Set<ManagedOperation> ops = mo.getOperations();
      log.debug(ops);

      String javaSpecVersion = System.getProperty("java.specification.version");
      if (javaSpecVersion.equals("1.5") || javaSpecVersion.equals("5.0"))
         assertEquals(mo + " has wrong number of ManagedOperations.", 8, ops.size());
      else if (javaSpecVersion.equals("1.6") || javaSpecVersion.equals("6.0"))
         assertEquals(mo + " has wrong number of ManagedOperations.", 11, ops.size());
     
      ManagedOperation getThreadInfo = ManagedOperationMatcher.findOperation(ops,
            "getThreadInfo", SimpleMetaType.LONG_PRIMITIVE, SimpleMetaType.INTEGER_PRIMITIVE);
      assertNotNull("getThreadInfo", getThreadInfo);
      log.debug(getThreadInfo);
      String[] getThreadInfoSig = getThreadInfo.getReflectionSignature();
      String[] getThreadInfoSigExpected = {"long", "int"};
      assertEquals(Arrays.asList(getThreadInfoSigExpected), Arrays.asList(getThreadInfoSig));
      ManagedOperation resetPeakThreadCount = ManagedOperationMatcher.findOperation(ops,
            "resetPeakThreadCount");
      assertNotNull("resetPeakThreadCount", resetPeakThreadCount);
      assertEquals(0, resetPeakThreadCount.getReflectionSignature().length);

      MBeanServer server = ManagementFactory.getPlatformMBeanServer();
      ObjectName tname = new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME);
      MBeanInfo tinfo = server.getMBeanInfo(tname);
      MBeanOperationInfo[] tops = tinfo.getOperations();
      if (javaSpecVersion.equals("1.5") || javaSpecVersion.equals("5.0"))
         assertEquals(8, tops.length);
      else if (javaSpecVersion.equals("1.6") || javaSpecVersion.equals("6.0"))
         assertEquals(11, tops.length);
      for(MBeanOperationInfo op : tops)
      {
         MBeanParameterInfo[] params = op.getSignature();
         String sig = "";
         if(params != null)
         {
            for(MBeanParameterInfo param : params)
            {
               if(sig.length() > 0)
                  sig += ",";
               sig += param.getType();
            }
         }
         log.debug(op.getName()+"("+sig+")");
      }
      Object[] params = {threadID};
      String[] signature = {"long"};
      Object result = server.invoke(tname, "getThreadInfo", params, signature);
      threadInfo = mbean.getThreadInfo(threadID);
      log.debug("getThreadInfo()-OpenType: "+result);
      assertTrue(result instanceof CompositeDataSupport);
      MetaValue resultMV = metaValueFactory.create(result);
      assertTrue(resultMV instanceof CompositeValue);
      CompositeValue resultCV = (CompositeValue) resultMV;
      log.debug("getThreadInfo()-MetaType: "+resultCV);
     
      ThreadInfo resultTI = ManagementFactoryUtils.unwrapThreadInfo(resultCV);
      threadInfo2 = resultTI;
      assertEquals(threadInfo.getLockOwnerName(), threadInfo2.getLockOwnerName());
      assertEquals(threadInfo.getThreadName(), threadInfo2.getThreadName());
      assertEquals(threadInfo.isInNative(), threadInfo2.isInNative());
      assertEquals(threadInfo.getBlockedCount(), threadInfo2.getBlockedCount());
View Full Code Here

     * @param t Thread
     * @param pw PrintWriter
     */
    static public void reportThread(Thread t, PrintWriter pw) {
        ThreadMXBean tmxb = ManagementFactory.getThreadMXBean();
        ThreadInfo info = tmxb.getThreadInfo(t.getId());
        pw.print("Java Thread State: ");
        pw.println(info.getThreadState());
        pw.print("Blocked/Waiting On: ");
        if (info.getLockOwnerId() >= 0) {
            pw.print(info.getLockName());
            pw.print(" which is owned by ");
            pw.print(info.getLockOwnerName());
            pw.print("(");
            pw.print(info.getLockOwnerId());
            pw.println(")");
        } else {
            pw.println("NONE");
        }
       
View Full Code Here

        }
        return untyped;
    }

    public StackTraceElement[] retrieveStackTrace(long threadId, int maxStackTraceDepth) {
        ThreadInfo extendedInfo = threadMXBean.getThreadInfo(threadId, maxStackTraceDepth);
        if (extendedInfo != null) {
            return extendedInfo.getStackTrace();
        }
        else
            return new StackTraceElement[0];
    }
View Full Code Here

        return null;
    }

    void printThread(ThreadMXBean threadsBean, Long id) {
        threadsBean.setThreadCpuTimeEnabled(true);
        ThreadInfo ti = threadsBean.getThreadInfo(id, Integer.MAX_VALUE);
        System.out.println("Thread " + ti.getThreadId() + " " + ti.getThreadName() + " " + ti.getThreadState());
        System.out.println("Stacktrace:");
        StackTraceElement[] st = ti.getStackTrace();
        for (StackTraceElement ste : st) {
            System.out.println(ste.getClassName() + "." + ste.getMethodName() + " line: " + ste.getLineNumber());
        }
    }
View Full Code Here

    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid: threadIds) {
      ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
      if (info == null) {
        stream.println("  Inactive");
        continue;
      }
      stream.println("Thread " +
                     getTaskName(info.getThreadId(),
                                 info.getThreadName()) + ":");
      Thread.State state = info.getThreadState();
      stream.println("  State: " + state);
      stream.println("  Blocked count: " + info.getBlockedCount());
      stream.println("  Waited count: " + info.getWaitedCount());
      if (contention) {
        stream.println("  Blocked time: " + info.getBlockedTime());
        stream.println("  Waited time: " + info.getWaitedTime());
      }
      if (state == Thread.State.WAITING) {
        stream.println("  Waiting on " + info.getLockName());
      } else  if (state == Thread.State.BLOCKED) {
        stream.println("  Blocked on " + info.getLockName());
        stream.println("  Blocked by " +
                       getTaskName(info.getLockOwnerId(),
                                   info.getLockOwnerName()));
      }
      stream.println("  Stack:");
      for (StackTraceElement frame: info.getStackTrace()) {
        stream.println("    " + frame.toString());
      }
    }
    stream.flush();
  }
View Full Code Here

TOP

Related Classes of java.lang.management.ThreadInfo

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.