001package com.hfg.util;
002
003
004//------------------------------------------------------------------------------
005
006import java.util.ArrayList;
007import java.util.List;
008
009/**
010 Thread-related utility functions.
011 <div>
012 @author J. Alex Taylor, hairyfatguy.com
013 </div>
014 */
015//------------------------------------------------------------------------------
016// com.hfg XML/HTML Coding Library
017//
018// This library is free software; you can redistribute it and/or
019// modify it under the terms of the GNU Lesser General Public
020// License as published by the Free Software Foundation; either
021// version 2.1 of the License, or (at your option) any later version.
022//
023// This library is distributed in the hope that it will be useful,
024// but WITHOUT ANY WARRANTY; without even the implied warranty of
025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
026// Lesser General Public License for more details.
027//
028// You should have received a copy of the GNU Lesser General Public
029// License along with this library; if not, write to the Free Software
030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
031//
032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
033// jataylor@hairyfatguy.com
034//------------------------------------------------------------------------------
035
036public class ThreadUtil
037{
038   //--------------------------------------------------------------------------
039   public static void sleep(long inMilliseconds)
040   {
041      try
042      {
043         Thread.sleep(inMilliseconds);
044      }
045      catch (InterruptedException e)
046      {
047         // Ignore
048      }
049   }
050
051   //--------------------------------------------------------------------------
052   public static ThreadGroup getThreadRoot()
053   {
054      // Wander up the thread group hierarchy to the top group
055      ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
056      ThreadGroup parentGroup;
057      while ((parentGroup = rootGroup.getParent()) != null)
058      {
059         rootGroup = parentGroup;
060      }
061
062      return rootGroup;
063   }
064
065   //--------------------------------------------------------------------------
066   public static List<ThreadGroup> getAllThreadGroups()
067   {
068      ThreadGroup rootGroup = getThreadRoot();
069      ThreadGroup[] threadGroupArray = new ThreadGroup[rootGroup.activeGroupCount()];
070      while (rootGroup.enumerate(threadGroupArray, true) == threadGroupArray.length)
071      {
072         threadGroupArray = new ThreadGroup[threadGroupArray.length * 2];
073      }
074
075      List<ThreadGroup> threadGroups = new ArrayList<>(threadGroupArray.length);
076      threadGroups.add(rootGroup);
077
078      for (ThreadGroup threadGroup : threadGroupArray)
079      {
080         if (threadGroup != null)
081         {
082            threadGroups.add(threadGroup);
083         }
084      }
085
086      return threadGroups;
087   }
088
089   //--------------------------------------------------------------------------
090   public static ThreadGroup getThreadGroup(String inName)
091   {
092      ThreadGroup requestedThreadGroup = null;
093
094      for (ThreadGroup threadGroup : getAllThreadGroups())
095      {
096         if (threadGroup.getName().equals(inName))
097         {
098            requestedThreadGroup = threadGroup;
099            break;
100         }
101      }
102
103      return requestedThreadGroup;
104   }
105}