Java Code Examples for com.helger.commons.collection.ArrayHelper#isNotEmpty()

The following examples show how to use com.helger.commons.collection.ArrayHelper#isNotEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadDescriptor.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
public String getLockInfo ()
{
  final StringBuilder aSB = new StringBuilder ();
  if (m_aThreadInfo != null)
    try
    {
      final MonitorInfo [] aMonitorInfos = m_aThreadInfo.getLockedMonitors ();
      if (ArrayHelper.isNotEmpty (aMonitorInfos))
      {
        aSB.append ("Information on ").append (aMonitorInfos.length).append (" monitors:\n");
        for (final MonitorInfo aMonitorInfo : aMonitorInfos)
        {
          aSB.append ("  monitor: ")
             .append (aMonitorInfo.getClassName ())
             .append ('@')
             .append (Integer.toHexString (aMonitorInfo.getIdentityHashCode ()))
             .append (" at ")
             .append (aMonitorInfo.getLockedStackFrame ())
             .append (" [")
             .append ((aMonitorInfo.getLockedStackDepth ()))
             .append ("]\n");
        }
      }
      final LockInfo [] aSynchronizers = m_aThreadInfo.getLockedSynchronizers ();
      if (ArrayHelper.isNotEmpty (aSynchronizers))
      {
        aSB.append ("Information on ").append (aSynchronizers.length).append (" synchronizers:\n");
        for (final LockInfo aSynchronizer : aSynchronizers)
        {
          aSB.append ("  lock:")
             .append (aSynchronizer.getClassName ())
             .append ('@')
             .append (Integer.toHexString (aSynchronizer.getIdentityHashCode ()))
             .append ('\n');
        }
      }
    }
    catch (final Exception ex)
    {
      aSB.append ("Error retrieving infos: ").append (ex.toString ());
    }
  return aSB.toString ();
}
 
Example 2
Source File: ThreadDescriptor.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
@Nonnull
public IMicroElement getAsMicroNode ()
{
  final IMicroElement eRet = new MicroElement ("thread");
  eRet.setAttribute ("id", m_nID);
  eRet.setAttribute ("name", m_sName);
  if (m_eState != null)
    eRet.setAttribute ("state", m_eState.toString ());
  eRet.setAttribute ("priority", m_nPriority);
  eRet.setAttribute ("threadgroup", m_sThreadGroup);
  eRet.appendElement ("stacktrace").appendText (getStackTraceNotNull ());
  if (m_aThreadInfo != null)
  {
    final IMicroElement eThreadInfo = eRet.appendElement ("threadinfo");
    try
    {
      final MonitorInfo [] aMonitorInfos = m_aThreadInfo.getLockedMonitors ();
      if (ArrayHelper.isNotEmpty (aMonitorInfos))
      {
        final IMicroElement eMonitorInfos = eThreadInfo.appendElement ("monitorinfos");
        eMonitorInfos.setAttribute ("count", aMonitorInfos.length);
        for (final MonitorInfo aMonitorInfo : aMonitorInfos)
        {
          final IMicroElement eMonitor = eMonitorInfos.appendElement ("monitor");
          eMonitor.setAttribute ("classname", aMonitorInfo.getClassName ());
          eMonitor.setAttribute ("identity", Integer.toHexString (aMonitorInfo.getIdentityHashCode ()));
          if (aMonitorInfo.getLockedStackFrame () != null)
            eMonitor.setAttribute ("stackframe", aMonitorInfo.getLockedStackFrame ().toString ());
          if (aMonitorInfo.getLockedStackDepth () >= 0)
            eMonitor.setAttribute ("stackdepth", aMonitorInfo.getLockedStackDepth ());
        }
      }

      final LockInfo [] aSynchronizers = m_aThreadInfo.getLockedSynchronizers ();
      if (ArrayHelper.isNotEmpty (aSynchronizers))
      {
        final IMicroElement eSynchronizers = eThreadInfo.appendElement ("synchronizers");
        eSynchronizers.setAttribute ("count", aSynchronizers.length);
        for (final LockInfo aSynchronizer : aSynchronizers)
        {
          final IMicroElement eSynchronizer = eSynchronizers.appendElement ("synchronizer");
          eSynchronizer.setAttribute ("classname", aSynchronizer.getClassName ());
          eSynchronizer.setAttribute ("identity", Integer.toHexString (aSynchronizer.getIdentityHashCode ()));
        }
      }
    }
    catch (final Exception ex)
    {
      eThreadInfo.setAttribute ("error", ex.getMessage ()).appendText (StackTraceHelper.getStackAsString (ex));
    }
  }
  return eRet;
}
 
Example 3
Source File: ThreadDeadlockDetector.java    From ph-commons with Apache License 2.0 4 votes vote down vote up
/**
 * This is the main method to be invoked to find deadlocked threads. In case a
 * deadlock is found, all registered callbacks are invoked.
 */
public void findDeadlockedThreads ()
{
  final long [] aThreadIDs = m_aMBean.isSynchronizerUsageSupported () ? m_aMBean.findDeadlockedThreads ()
                                                                      : m_aMBean.findMonitorDeadlockedThreads ();
  if (ArrayHelper.isNotEmpty (aThreadIDs))
  {
    // Get all stack traces
    final Map <Thread, StackTraceElement []> aAllStackTraces = Thread.getAllStackTraces ();

    // Sort by ID for a consistent result
    Arrays.sort (aThreadIDs);

    // Extract the relevant information
    final ThreadDeadlockInfo [] aThreadInfos = new ThreadDeadlockInfo [aThreadIDs.length];
    for (int i = 0; i < aThreadInfos.length; i++)
    {
      // ThreadInfo
      final ThreadInfo aThreadInfo = m_aMBean.getThreadInfo (aThreadIDs[i]);

      // Find matching thread and stack trace
      Thread aFoundThread = null;
      StackTraceElement [] aFoundStackTrace = null;
      // Sort ascending by thread ID for a consistent result
      for (final Map.Entry <Thread, StackTraceElement []> aEnrty : aAllStackTraces.entrySet ())
        if (aEnrty.getKey ().getId () == aThreadInfo.getThreadId ())
        {
          aFoundThread = aEnrty.getKey ();
          aFoundStackTrace = aEnrty.getValue ();
          break;
        }
      if (aFoundThread == null)
        throw new IllegalStateException ("Deadlocked Thread not found as defined by " + aThreadInfo.toString ());

      // Remember
      aThreadInfos[i] = new ThreadDeadlockInfo (aThreadInfo, aFoundThread, aFoundStackTrace);
    }

    // Invoke all callbacks
    if (m_aCallbacks.isEmpty ())
    {
      if (LOGGER.isWarnEnabled ())
        LOGGER.warn ("Found a deadlock of " + aThreadInfos.length + " threads but no callbacks are present!");
    }
    else
      m_aCallbacks.forEach (x -> x.onDeadlockDetected (aThreadInfos));
  }
}