ThreadPoolManager.java 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver;
  20. import java.lang.Thread.UncaughtExceptionHandler;
  21. import java.util.concurrent.LinkedBlockingQueue;
  22. import java.util.concurrent.RejectedExecutionException;
  23. import java.util.concurrent.ScheduledFuture;
  24. import java.util.concurrent.ScheduledThreadPoolExecutor;
  25. import java.util.concurrent.ThreadFactory;
  26. import java.util.concurrent.ThreadPoolExecutor;
  27. import java.util.concurrent.TimeUnit;
  28. import java.util.concurrent.atomic.AtomicInteger;
  29. import java.util.logging.Level;
  30. import java.util.logging.Logger;
  31. import javolution.util.FastList;
  32. import javolution.util.FastMap;
  33. import javolution.util.FastSet;
  34. import com.l2jserver.Config;
  35. import com.l2jserver.util.StringUtil;
  36. /**
  37. * <p>
  38. * This class is made to handle all the ThreadPools used in L2J.
  39. * </p>
  40. * <p>
  41. * Scheduled Tasks can either be sent to a {@link #_generalScheduledThreadPool "general"} or {@link #_effectsScheduledThreadPool "effects"} {@link ScheduledThreadPoolExecutor ScheduledThreadPool}: The "effects" one is used for every effects (skills, hp/mp regen ...) while the "general" one is used
  42. * for everything else that needs to be scheduled.<br>
  43. * There also is an {@link #_aiScheduledThreadPool "ai"} {@link ScheduledThreadPoolExecutor ScheduledThreadPool} used for AI Tasks.
  44. * </p>
  45. * <p>
  46. * Tasks can be sent to {@link ScheduledThreadPoolExecutor ScheduledThreadPool} either with:
  47. * <ul>
  48. * <li>{@link #scheduleEffect(Runnable, long, TimeUnit)} and {@link #scheduleEffect(Runnable, long)} : for effects Tasks that needs to be executed only once.</li>
  49. * <li>{@link #scheduleGeneral(Runnable, long, TimeUnit)} and {@link #scheduleGeneral(Runnable, long)} : for scheduled Tasks that needs to be executed once.</li>
  50. * <li>{@link #scheduleAi(Runnable, long, TimeUnit)} and {@link #scheduleAi(Runnable, long)} : for AI Tasks that needs to be executed once</li>
  51. * </ul>
  52. * or
  53. * <ul>
  54. * <li>{@link #scheduleEffectAtFixedRate(Runnable, long, long, TimeUnit)} and {@link #scheduleEffectAtFixedRate(Runnable, long, long)} : for effects Tasks that needs to be executed periodicaly.</li>
  55. * <li>{@link #scheduleGeneralAtFixedRate(Runnable, long, long, TimeUnit)} and {@link #scheduleGeneralAtFixedRate(Runnable, long, long)} : for scheduled Tasks that needs to be executed periodicaly.</li>
  56. * <li>{@link #scheduleAiAtFixedRate(Runnable, long, long, TimeUnit)} and {@link #scheduleAiAtFixedRate(Runnable, long, long)} : for AI Tasks that needs to be executed periodicaly</li>
  57. * </ul>
  58. * </p>
  59. * <p>
  60. * For all Tasks that should be executed with no delay asynchronously in a ThreadPool there also are usual {@link ThreadPoolExecutor ThreadPools} that can grow/shrink according to their load.:
  61. * <ul>
  62. * <li>{@link #_generalPacketsThreadPool GeneralPackets} where most packets handler are executed.</li>
  63. * <li>{@link #_ioPacketsThreadPool I/O Packets} where all the i/o packets are executed.</li>
  64. * <li>There will be an AI ThreadPool where AI events should be executed</li>
  65. * <li>A general ThreadPool where everything else that needs to run asynchronously with no delay should be executed ({@link com.l2jserver.gameserver.model.actor.knownlist KnownList} updates, SQL updates/inserts...)?</li>
  66. * </ul>
  67. * </p>
  68. * @author -Wooden-
  69. */
  70. public class ThreadPoolManager
  71. {
  72. protected static final Logger _log = Logger.getLogger(ThreadPoolManager.class.getName());
  73. private static final class RunnableWrapper implements Runnable
  74. {
  75. private final Runnable _r;
  76. public RunnableWrapper(final Runnable r)
  77. {
  78. _r = r;
  79. }
  80. @Override
  81. public final void run()
  82. {
  83. try
  84. {
  85. _r.run();
  86. }
  87. catch (final Throwable e)
  88. {
  89. final Thread t = Thread.currentThread();
  90. final UncaughtExceptionHandler h = t.getUncaughtExceptionHandler();
  91. if (h != null)
  92. {
  93. h.uncaughtException(t, e);
  94. }
  95. }
  96. }
  97. }
  98. protected ScheduledThreadPoolExecutor _effectsScheduledThreadPool;
  99. protected ScheduledThreadPoolExecutor _generalScheduledThreadPool;
  100. protected ScheduledThreadPoolExecutor _aiScheduledThreadPool;
  101. private final ThreadPoolExecutor _generalPacketsThreadPool;
  102. private final ThreadPoolExecutor _ioPacketsThreadPool;
  103. private final ThreadPoolExecutor _generalThreadPool;
  104. private boolean _shutdown;
  105. public static ThreadPoolManager getInstance()
  106. {
  107. return SingletonHolder._instance;
  108. }
  109. protected ThreadPoolManager()
  110. {
  111. _effectsScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.THREAD_P_EFFECTS, new PriorityThreadFactory("EffectsSTPool", Thread.NORM_PRIORITY));
  112. _generalScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.THREAD_P_GENERAL, new PriorityThreadFactory("GeneralSTPool", Thread.NORM_PRIORITY));
  113. _ioPacketsThreadPool = new ThreadPoolExecutor(Config.IO_PACKET_THREAD_CORE_SIZE, Integer.MAX_VALUE, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new PriorityThreadFactory("I/O Packet Pool", Thread.NORM_PRIORITY + 1));
  114. _generalPacketsThreadPool = new ThreadPoolExecutor(Config.GENERAL_PACKET_THREAD_CORE_SIZE, Config.GENERAL_PACKET_THREAD_CORE_SIZE + 2, 15L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new PriorityThreadFactory("Normal Packet Pool", Thread.NORM_PRIORITY + 1));
  115. _generalThreadPool = new ThreadPoolExecutor(Config.GENERAL_THREAD_CORE_SIZE, Config.GENERAL_THREAD_CORE_SIZE + 2, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new PriorityThreadFactory("General Pool", Thread.NORM_PRIORITY));
  116. _aiScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.AI_MAX_THREAD, new PriorityThreadFactory("AISTPool", Thread.NORM_PRIORITY));
  117. scheduleGeneralAtFixedRate(new PurgeTask(), 10, 5, TimeUnit.MINUTES);
  118. }
  119. /**
  120. * Schedules an effect task to be executed after the given delay.
  121. * @param task the task to execute
  122. * @param delay the delay in the given time unit
  123. * @param unit the time unit of the delay parameter
  124. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  125. */
  126. public ScheduledFuture<?> scheduleEffect(Runnable task, long delay, TimeUnit unit)
  127. {
  128. try
  129. {
  130. return _effectsScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  131. }
  132. catch (RejectedExecutionException e)
  133. {
  134. return null;
  135. }
  136. }
  137. /**
  138. * Schedules an effect task to be executed after the given delay.
  139. * @param task the task to execute
  140. * @param delay the delay in milliseconds
  141. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  142. */
  143. public ScheduledFuture<?> scheduleEffect(Runnable task, long delay)
  144. {
  145. return scheduleEffect(task, delay, TimeUnit.MILLISECONDS);
  146. }
  147. /**
  148. * Schedules an effect task to be executed at fixed rate.
  149. * @param task the task to execute
  150. * @param initialDelay the initial delay in the given time unit
  151. * @param period the period between executions in the given time unit
  152. * @param unit the time unit of the initialDelay and period parameters
  153. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  154. */
  155. public ScheduledFuture<?> scheduleEffectAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  156. {
  157. try
  158. {
  159. return _effectsScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  160. }
  161. catch (RejectedExecutionException e)
  162. {
  163. return null; /* shutdown, ignore */
  164. }
  165. }
  166. /**
  167. * Schedules an effect task to be executed at fixed rate.
  168. * @param task the task to execute
  169. * @param initialDelay the initial delay in milliseconds
  170. * @param period the period between executions in milliseconds
  171. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  172. */
  173. public ScheduledFuture<?> scheduleEffectAtFixedRate(Runnable task, long initialDelay, long period)
  174. {
  175. return scheduleEffectAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  176. }
  177. /**
  178. * Schedules a general task to be executed after the given delay.
  179. * @param task the task to execute
  180. * @param delay the delay in the given time unit
  181. * @param unit the time unit of the delay parameter
  182. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  183. */
  184. public ScheduledFuture<?> scheduleGeneral(Runnable task, long delay, TimeUnit unit)
  185. {
  186. try
  187. {
  188. return _generalScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  189. }
  190. catch (RejectedExecutionException e)
  191. {
  192. return null; /* shutdown, ignore */
  193. }
  194. }
  195. /**
  196. * Schedules a general task to be executed after the given delay.
  197. * @param task the task to execute
  198. * @param delay the delay in milliseconds
  199. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  200. */
  201. public ScheduledFuture<?> scheduleGeneral(Runnable task, long delay)
  202. {
  203. return scheduleGeneral(task, delay, TimeUnit.MILLISECONDS);
  204. }
  205. /**
  206. * Schedules a general task to be executed at fixed rate.
  207. * @param task the task to execute
  208. * @param initialDelay the initial delay in the given time unit
  209. * @param period the period between executions in the given time unit
  210. * @param unit the time unit of the initialDelay and period parameters
  211. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  212. */
  213. public ScheduledFuture<?> scheduleGeneralAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  214. {
  215. try
  216. {
  217. return _generalScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  218. }
  219. catch (RejectedExecutionException e)
  220. {
  221. return null; /* shutdown, ignore */
  222. }
  223. }
  224. /**
  225. * Schedules a general task to be executed at fixed rate.
  226. * @param task the task to execute
  227. * @param initialDelay the initial delay in milliseconds
  228. * @param period the period between executions in milliseconds
  229. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  230. */
  231. public ScheduledFuture<?> scheduleGeneralAtFixedRate(Runnable task, long initialDelay, long period)
  232. {
  233. return scheduleGeneralAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  234. }
  235. /**
  236. * Schedules an AI task to be executed after the given delay.
  237. * @param task the task to execute
  238. * @param delay the delay in the given time unit
  239. * @param unit the time unit of the delay parameter
  240. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  241. */
  242. public ScheduledFuture<?> scheduleAi(Runnable task, long delay, TimeUnit unit)
  243. {
  244. try
  245. {
  246. return _aiScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  247. }
  248. catch (RejectedExecutionException e)
  249. {
  250. return null; /* shutdown, ignore */
  251. }
  252. }
  253. /**
  254. * Schedules an AI task to be executed after the given delay.
  255. * @param task the task to execute
  256. * @param delay the delay in milliseconds
  257. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  258. */
  259. public ScheduledFuture<?> scheduleAi(Runnable task, long delay)
  260. {
  261. return scheduleAi(task, delay, TimeUnit.MILLISECONDS);
  262. }
  263. /**
  264. * Schedules a general task to be executed at fixed rate.
  265. * @param task the task to execute
  266. * @param initialDelay the initial delay in the given time unit
  267. * @param period the period between executions in the given time unit
  268. * @param unit the time unit of the initialDelay and period parameters
  269. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  270. */
  271. public ScheduledFuture<?> scheduleAiAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  272. {
  273. try
  274. {
  275. return _aiScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  276. }
  277. catch (RejectedExecutionException e)
  278. {
  279. return null; /* shutdown, ignore */
  280. }
  281. }
  282. /**
  283. * Schedules a general task to be executed at fixed rate.
  284. * @param task the task to execute
  285. * @param initialDelay the initial delay in milliseconds
  286. * @param period the period between executions in milliseconds
  287. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  288. */
  289. public ScheduledFuture<?> scheduleAiAtFixedRate(Runnable task, long initialDelay, long period)
  290. {
  291. return scheduleAiAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  292. }
  293. /**
  294. * Executes a packet task sometime in future in another thread.
  295. * @param task the task to execute
  296. */
  297. public void executePacket(Runnable task)
  298. {
  299. _generalPacketsThreadPool.execute(task);
  300. }
  301. /**
  302. * Executes an IO packet task sometime in future in another thread.
  303. * @param task the task to execute
  304. */
  305. public void executeIOPacket(Runnable task)
  306. {
  307. _ioPacketsThreadPool.execute(task);
  308. }
  309. /**
  310. * Executes a general task sometime in future in another thread.
  311. * @param task the task to execute
  312. */
  313. public void executeGeneral(Runnable task)
  314. {
  315. _generalThreadPool.execute(new RunnableWrapper(task));
  316. }
  317. /**
  318. * Executes an AI task sometime in future in another thread.
  319. * @param task the task to execute
  320. */
  321. public void executeAi(Runnable task)
  322. {
  323. _aiScheduledThreadPool.execute(new RunnableWrapper(task));
  324. }
  325. public String[] getStats()
  326. {
  327. return new String[]
  328. {
  329. "STP:",
  330. " + Effects:",
  331. " |- ActiveThreads: " + _effectsScheduledThreadPool.getActiveCount(),
  332. " |- getCorePoolSize: " + _effectsScheduledThreadPool.getCorePoolSize(),
  333. " |- PoolSize: " + _effectsScheduledThreadPool.getPoolSize(),
  334. " |- MaximumPoolSize: " + _effectsScheduledThreadPool.getMaximumPoolSize(),
  335. " |- CompletedTasks: " + _effectsScheduledThreadPool.getCompletedTaskCount(),
  336. " |- ScheduledTasks: " + (_effectsScheduledThreadPool.getTaskCount() - _effectsScheduledThreadPool.getCompletedTaskCount()),
  337. " | -------",
  338. " + General:",
  339. " |- ActiveThreads: " + _generalScheduledThreadPool.getActiveCount(),
  340. " |- getCorePoolSize: " + _generalScheduledThreadPool.getCorePoolSize(),
  341. " |- PoolSize: " + _generalScheduledThreadPool.getPoolSize(),
  342. " |- MaximumPoolSize: " + _generalScheduledThreadPool.getMaximumPoolSize(),
  343. " |- CompletedTasks: " + _generalScheduledThreadPool.getCompletedTaskCount(),
  344. " |- ScheduledTasks: " + (_generalScheduledThreadPool.getTaskCount() - _generalScheduledThreadPool.getCompletedTaskCount()),
  345. " | -------",
  346. " + AI:",
  347. " |- ActiveThreads: " + _aiScheduledThreadPool.getActiveCount(),
  348. " |- getCorePoolSize: " + _aiScheduledThreadPool.getCorePoolSize(),
  349. " |- PoolSize: " + _aiScheduledThreadPool.getPoolSize(),
  350. " |- MaximumPoolSize: " + _aiScheduledThreadPool.getMaximumPoolSize(),
  351. " |- CompletedTasks: " + _aiScheduledThreadPool.getCompletedTaskCount(),
  352. " |- ScheduledTasks: " + (_aiScheduledThreadPool.getTaskCount() - _aiScheduledThreadPool.getCompletedTaskCount()),
  353. "TP:",
  354. " + Packets:",
  355. " |- ActiveThreads: " + _generalPacketsThreadPool.getActiveCount(),
  356. " |- getCorePoolSize: " + _generalPacketsThreadPool.getCorePoolSize(),
  357. " |- MaximumPoolSize: " + _generalPacketsThreadPool.getMaximumPoolSize(),
  358. " |- LargestPoolSize: " + _generalPacketsThreadPool.getLargestPoolSize(),
  359. " |- PoolSize: " + _generalPacketsThreadPool.getPoolSize(),
  360. " |- CompletedTasks: " + _generalPacketsThreadPool.getCompletedTaskCount(),
  361. " |- QueuedTasks: " + _generalPacketsThreadPool.getQueue().size(),
  362. " | -------",
  363. " + I/O Packets:",
  364. " |- ActiveThreads: " + _ioPacketsThreadPool.getActiveCount(),
  365. " |- getCorePoolSize: " + _ioPacketsThreadPool.getCorePoolSize(),
  366. " |- MaximumPoolSize: " + _ioPacketsThreadPool.getMaximumPoolSize(),
  367. " |- LargestPoolSize: " + _ioPacketsThreadPool.getLargestPoolSize(),
  368. " |- PoolSize: " + _ioPacketsThreadPool.getPoolSize(),
  369. " |- CompletedTasks: " + _ioPacketsThreadPool.getCompletedTaskCount(),
  370. " |- QueuedTasks: " + _ioPacketsThreadPool.getQueue().size(),
  371. " | -------",
  372. " + General Tasks:",
  373. " |- ActiveThreads: " + _generalThreadPool.getActiveCount(),
  374. " |- getCorePoolSize: " + _generalThreadPool.getCorePoolSize(),
  375. " |- MaximumPoolSize: " + _generalThreadPool.getMaximumPoolSize(),
  376. " |- LargestPoolSize: " + _generalThreadPool.getLargestPoolSize(),
  377. " |- PoolSize: " + _generalThreadPool.getPoolSize(),
  378. " |- CompletedTasks: " + _generalThreadPool.getCompletedTaskCount(),
  379. " |- QueuedTasks: " + _generalThreadPool.getQueue().size(),
  380. " | -------",
  381. " + Javolution stats:",
  382. " |- FastList: " + FastList.report(),
  383. " |- FastMap: " + FastMap.report(),
  384. " |- FastSet: " + FastSet.report(),
  385. " | -------"
  386. };
  387. }
  388. private static class PriorityThreadFactory implements ThreadFactory
  389. {
  390. private final int _prio;
  391. private final String _name;
  392. private final AtomicInteger _threadNumber = new AtomicInteger(1);
  393. private final ThreadGroup _group;
  394. public PriorityThreadFactory(String name, int prio)
  395. {
  396. _prio = prio;
  397. _name = name;
  398. _group = new ThreadGroup(_name);
  399. }
  400. @Override
  401. public Thread newThread(Runnable r)
  402. {
  403. Thread t = new Thread(_group, r, _name + "-" + _threadNumber.getAndIncrement());
  404. t.setPriority(_prio);
  405. return t;
  406. }
  407. public ThreadGroup getGroup()
  408. {
  409. return _group;
  410. }
  411. }
  412. public void shutdown()
  413. {
  414. _shutdown = true;
  415. try
  416. {
  417. _effectsScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  418. _generalScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  419. _generalPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  420. _ioPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  421. _generalThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  422. _effectsScheduledThreadPool.shutdown();
  423. _generalScheduledThreadPool.shutdown();
  424. _generalPacketsThreadPool.shutdown();
  425. _ioPacketsThreadPool.shutdown();
  426. _generalThreadPool.shutdown();
  427. _log.info("All ThreadPools are now stopped");
  428. }
  429. catch (InterruptedException e)
  430. {
  431. _log.log(Level.WARNING, "", e);
  432. }
  433. }
  434. public boolean isShutdown()
  435. {
  436. return _shutdown;
  437. }
  438. public void purge()
  439. {
  440. _effectsScheduledThreadPool.purge();
  441. _generalScheduledThreadPool.purge();
  442. _aiScheduledThreadPool.purge();
  443. _ioPacketsThreadPool.purge();
  444. _generalPacketsThreadPool.purge();
  445. _generalThreadPool.purge();
  446. }
  447. public String getPacketStats()
  448. {
  449. final StringBuilder sb = new StringBuilder(1000);
  450. ThreadFactory tf = _generalPacketsThreadPool.getThreadFactory();
  451. if (tf instanceof PriorityThreadFactory)
  452. {
  453. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  454. int count = ptf.getGroup().activeCount();
  455. Thread[] threads = new Thread[count + 2];
  456. ptf.getGroup().enumerate(threads);
  457. StringUtil.append(sb, "General Packet Thread Pool:" + Config.EOL + "Tasks in the queue: ", String.valueOf(_generalPacketsThreadPool.getQueue().size()), Config.EOL + "Showing threads stack trace:" + Config.EOL + "There should be ", String.valueOf(count), " Threads" + Config.EOL);
  458. for (Thread t : threads)
  459. {
  460. if (t == null)
  461. {
  462. continue;
  463. }
  464. StringUtil.append(sb, t.getName(), Config.EOL);
  465. for (StackTraceElement ste : t.getStackTrace())
  466. {
  467. StringUtil.append(sb, ste.toString(), Config.EOL);
  468. }
  469. }
  470. }
  471. sb.append("Packet Tp stack traces printed.");
  472. sb.append(Config.EOL);
  473. return sb.toString();
  474. }
  475. public String getIOPacketStats()
  476. {
  477. final StringBuilder sb = new StringBuilder(1000);
  478. ThreadFactory tf = _ioPacketsThreadPool.getThreadFactory();
  479. if (tf instanceof PriorityThreadFactory)
  480. {
  481. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  482. int count = ptf.getGroup().activeCount();
  483. Thread[] threads = new Thread[count + 2];
  484. ptf.getGroup().enumerate(threads);
  485. StringUtil.append(sb, "I/O Packet Thread Pool:" + Config.EOL + "Tasks in the queue: ", String.valueOf(_ioPacketsThreadPool.getQueue().size()), Config.EOL + "Showing threads stack trace:" + Config.EOL + "There should be ", String.valueOf(count), " Threads" + Config.EOL);
  486. for (Thread t : threads)
  487. {
  488. if (t == null)
  489. {
  490. continue;
  491. }
  492. StringUtil.append(sb, t.getName(), Config.EOL);
  493. for (StackTraceElement ste : t.getStackTrace())
  494. {
  495. StringUtil.append(sb, ste.toString(), Config.EOL);
  496. }
  497. }
  498. }
  499. sb.append("Packet Tp stack traces printed." + Config.EOL);
  500. return sb.toString();
  501. }
  502. public String getGeneralStats()
  503. {
  504. final StringBuilder sb = new StringBuilder(1000);
  505. ThreadFactory tf = _generalThreadPool.getThreadFactory();
  506. if (tf instanceof PriorityThreadFactory)
  507. {
  508. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  509. int count = ptf.getGroup().activeCount();
  510. Thread[] threads = new Thread[count + 2];
  511. ptf.getGroup().enumerate(threads);
  512. StringUtil.append(sb, "General Thread Pool:" + Config.EOL + "Tasks in the queue: ", String.valueOf(_generalThreadPool.getQueue().size()), Config.EOL + "Showing threads stack trace:" + Config.EOL + "There should be ", String.valueOf(count), " Threads" + Config.EOL);
  513. for (Thread t : threads)
  514. {
  515. if (t == null)
  516. {
  517. continue;
  518. }
  519. StringUtil.append(sb, t.getName(), Config.EOL);
  520. for (StackTraceElement ste : t.getStackTrace())
  521. {
  522. StringUtil.append(sb, ste.toString(), Config.EOL);
  523. }
  524. }
  525. }
  526. sb.append("Packet Tp stack traces printed." + Config.EOL);
  527. return sb.toString();
  528. }
  529. protected class PurgeTask implements Runnable
  530. {
  531. @Override
  532. public void run()
  533. {
  534. _effectsScheduledThreadPool.purge();
  535. _generalScheduledThreadPool.purge();
  536. _aiScheduledThreadPool.purge();
  537. }
  538. }
  539. private static class SingletonHolder
  540. {
  541. protected static final ThreadPoolManager _instance = new ThreadPoolManager();
  542. }
  543. }