ThreadPoolManager.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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. protected ScheduledThreadPoolExecutor _eventScheduledThreadPool;
  102. private final ThreadPoolExecutor _generalPacketsThreadPool;
  103. private final ThreadPoolExecutor _ioPacketsThreadPool;
  104. private final ThreadPoolExecutor _generalThreadPool;
  105. private final ThreadPoolExecutor _eventThreadPool;
  106. private boolean _shutdown;
  107. public static ThreadPoolManager getInstance()
  108. {
  109. return SingletonHolder._instance;
  110. }
  111. protected ThreadPoolManager()
  112. {
  113. _effectsScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.THREAD_P_EFFECTS, new PriorityThreadFactory("EffectsSTPool", Thread.NORM_PRIORITY));
  114. _generalScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.THREAD_P_GENERAL, new PriorityThreadFactory("GeneralSTPool", Thread.NORM_PRIORITY));
  115. _eventScheduledThreadPool = new ScheduledThreadPoolExecutor(2, new PriorityThreadFactory("EventSTPool", Thread.NORM_PRIORITY));
  116. _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));
  117. _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));
  118. _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));
  119. _aiScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.AI_MAX_THREAD, new PriorityThreadFactory("AISTPool", Thread.NORM_PRIORITY));
  120. _eventThreadPool = new ThreadPoolExecutor(1, 2, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new PriorityThreadFactory("Event Pool", Thread.NORM_PRIORITY));
  121. scheduleGeneralAtFixedRate(new PurgeTask(), 10, 5, TimeUnit.MINUTES);
  122. }
  123. /**
  124. * Schedules an effect task to be executed after the given delay.
  125. * @param task the task to execute
  126. * @param delay the delay in the given time unit
  127. * @param unit the time unit of the delay parameter
  128. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  129. */
  130. public ScheduledFuture<?> scheduleEffect(Runnable task, long delay, TimeUnit unit)
  131. {
  132. try
  133. {
  134. return _effectsScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  135. }
  136. catch (RejectedExecutionException e)
  137. {
  138. return null;
  139. }
  140. }
  141. /**
  142. * Schedules an effect task to be executed after the given delay.
  143. * @param task the task to execute
  144. * @param delay the delay in milliseconds
  145. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  146. */
  147. public ScheduledFuture<?> scheduleEffect(Runnable task, long delay)
  148. {
  149. return scheduleEffect(task, delay, TimeUnit.MILLISECONDS);
  150. }
  151. /**
  152. * Schedules an effect task to be executed at fixed rate.
  153. * @param task the task to execute
  154. * @param initialDelay the initial delay in the given time unit
  155. * @param period the period between executions in the given time unit
  156. * @param unit the time unit of the initialDelay and period parameters
  157. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  158. */
  159. public ScheduledFuture<?> scheduleEffectAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  160. {
  161. try
  162. {
  163. return _effectsScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  164. }
  165. catch (RejectedExecutionException e)
  166. {
  167. return null; /* shutdown, ignore */
  168. }
  169. }
  170. /**
  171. * Schedules an effect task to be executed at fixed rate.
  172. * @param task the task to execute
  173. * @param initialDelay the initial delay in milliseconds
  174. * @param period the period between executions in milliseconds
  175. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  176. */
  177. public ScheduledFuture<?> scheduleEffectAtFixedRate(Runnable task, long initialDelay, long period)
  178. {
  179. return scheduleEffectAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  180. }
  181. /**
  182. * Schedules a general task to be executed after the given delay.
  183. * @param task the task to execute
  184. * @param delay the delay in the given time unit
  185. * @param unit the time unit of the delay parameter
  186. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  187. */
  188. public ScheduledFuture<?> scheduleGeneral(Runnable task, long delay, TimeUnit unit)
  189. {
  190. try
  191. {
  192. return _generalScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  193. }
  194. catch (RejectedExecutionException e)
  195. {
  196. return null; /* shutdown, ignore */
  197. }
  198. }
  199. /**
  200. * Schedules a general task to be executed after the given delay.
  201. * @param task the task to execute
  202. * @param delay the delay in milliseconds
  203. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  204. */
  205. public ScheduledFuture<?> scheduleGeneral(Runnable task, long delay)
  206. {
  207. return scheduleGeneral(task, delay, TimeUnit.MILLISECONDS);
  208. }
  209. /**
  210. * Schedules a general task to be executed at fixed rate.
  211. * @param task the task to execute
  212. * @param initialDelay the initial delay in the given time unit
  213. * @param period the period between executions in the given time unit
  214. * @param unit the time unit of the initialDelay and period parameters
  215. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  216. */
  217. public ScheduledFuture<?> scheduleGeneralAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  218. {
  219. try
  220. {
  221. return _generalScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  222. }
  223. catch (RejectedExecutionException e)
  224. {
  225. return null; /* shutdown, ignore */
  226. }
  227. }
  228. /**
  229. * Schedules a event task to be executed after the given delay.
  230. * @param task the task to execute
  231. * @param delay the delay in the given time unit
  232. * @param unit the time unit of the delay parameter
  233. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  234. */
  235. public ScheduledFuture<?> scheduleEvent(Runnable task, long delay, TimeUnit unit)
  236. {
  237. try
  238. {
  239. return _eventScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  240. }
  241. catch (RejectedExecutionException e)
  242. {
  243. return null; /* shutdown, ignore */
  244. }
  245. }
  246. /**
  247. * Schedules a event task to be executed after the given delay.
  248. * @param task the task to execute
  249. * @param delay the delay in milliseconds
  250. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  251. */
  252. public ScheduledFuture<?> scheduleEvent(Runnable task, long delay)
  253. {
  254. return scheduleEvent(task, delay, TimeUnit.MILLISECONDS);
  255. }
  256. /**
  257. * Schedules a event task to be executed at fixed rate.
  258. * @param task the task to execute
  259. * @param initialDelay the initial delay in the given time unit
  260. * @param period the period between executions in the given time unit
  261. * @param unit the time unit of the initialDelay and period parameters
  262. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  263. */
  264. public ScheduledFuture<?> scheduleEventAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  265. {
  266. try
  267. {
  268. return _eventScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  269. }
  270. catch (RejectedExecutionException e)
  271. {
  272. return null; /* shutdown, ignore */
  273. }
  274. }
  275. /**
  276. * Schedules a general task to be executed at fixed rate.
  277. * @param task the task to execute
  278. * @param initialDelay the initial delay in milliseconds
  279. * @param period the period between executions in milliseconds
  280. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  281. */
  282. public ScheduledFuture<?> scheduleGeneralAtFixedRate(Runnable task, long initialDelay, long period)
  283. {
  284. return scheduleGeneralAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  285. }
  286. /**
  287. * Schedules an AI task to be executed after the given delay.
  288. * @param task the task to execute
  289. * @param delay the delay in the given time unit
  290. * @param unit the time unit of the delay parameter
  291. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  292. */
  293. public ScheduledFuture<?> scheduleAi(Runnable task, long delay, TimeUnit unit)
  294. {
  295. try
  296. {
  297. return _aiScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  298. }
  299. catch (RejectedExecutionException e)
  300. {
  301. return null; /* shutdown, ignore */
  302. }
  303. }
  304. /**
  305. * Schedules an AI task to be executed after the given delay.
  306. * @param task the task to execute
  307. * @param delay the delay in milliseconds
  308. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  309. */
  310. public ScheduledFuture<?> scheduleAi(Runnable task, long delay)
  311. {
  312. return scheduleAi(task, delay, TimeUnit.MILLISECONDS);
  313. }
  314. /**
  315. * Schedules a general task to be executed at fixed rate.
  316. * @param task the task to execute
  317. * @param initialDelay the initial delay in the given time unit
  318. * @param period the period between executions in the given time unit
  319. * @param unit the time unit of the initialDelay and period parameters
  320. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  321. */
  322. public ScheduledFuture<?> scheduleAiAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  323. {
  324. try
  325. {
  326. return _aiScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  327. }
  328. catch (RejectedExecutionException e)
  329. {
  330. return null; /* shutdown, ignore */
  331. }
  332. }
  333. /**
  334. * Schedules a general task to be executed at fixed rate.
  335. * @param task the task to execute
  336. * @param initialDelay the initial delay in milliseconds
  337. * @param period the period between executions in milliseconds
  338. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  339. */
  340. public ScheduledFuture<?> scheduleAiAtFixedRate(Runnable task, long initialDelay, long period)
  341. {
  342. return scheduleAiAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  343. }
  344. /**
  345. * Executes a packet task sometime in future in another thread.
  346. * @param task the task to execute
  347. */
  348. public void executePacket(Runnable task)
  349. {
  350. _generalPacketsThreadPool.execute(task);
  351. }
  352. /**
  353. * Executes an IO packet task sometime in future in another thread.
  354. * @param task the task to execute
  355. */
  356. public void executeIOPacket(Runnable task)
  357. {
  358. _ioPacketsThreadPool.execute(task);
  359. }
  360. /**
  361. * Executes a general task sometime in future in another thread.
  362. * @param task the task to execute
  363. */
  364. public void executeGeneral(Runnable task)
  365. {
  366. _generalThreadPool.execute(new RunnableWrapper(task));
  367. }
  368. /**
  369. * Executes an AI task sometime in future in another thread.
  370. * @param task the task to execute
  371. */
  372. public void executeAi(Runnable task)
  373. {
  374. _aiScheduledThreadPool.execute(new RunnableWrapper(task));
  375. }
  376. /**
  377. * Executes an Event task sometime in future in another thread.
  378. * @param task the task to execute
  379. */
  380. public void executeEvent(Runnable task)
  381. {
  382. _eventThreadPool.execute(new RunnableWrapper(task));
  383. }
  384. public String[] getStats()
  385. {
  386. return new String[]
  387. {
  388. "STP:",
  389. " + Effects:",
  390. " |- ActiveThreads: " + _effectsScheduledThreadPool.getActiveCount(),
  391. " |- getCorePoolSize: " + _effectsScheduledThreadPool.getCorePoolSize(),
  392. " |- PoolSize: " + _effectsScheduledThreadPool.getPoolSize(),
  393. " |- MaximumPoolSize: " + _effectsScheduledThreadPool.getMaximumPoolSize(),
  394. " |- CompletedTasks: " + _effectsScheduledThreadPool.getCompletedTaskCount(),
  395. " |- ScheduledTasks: " + _effectsScheduledThreadPool.getQueue().size(),
  396. " | -------",
  397. " + General:",
  398. " |- ActiveThreads: " + _generalScheduledThreadPool.getActiveCount(),
  399. " |- getCorePoolSize: " + _generalScheduledThreadPool.getCorePoolSize(),
  400. " |- PoolSize: " + _generalScheduledThreadPool.getPoolSize(),
  401. " |- MaximumPoolSize: " + _generalScheduledThreadPool.getMaximumPoolSize(),
  402. " |- CompletedTasks: " + _generalScheduledThreadPool.getCompletedTaskCount(),
  403. " |- ScheduledTasks: " + _generalScheduledThreadPool.getQueue().size(),
  404. " | -------",
  405. " + AI:",
  406. " |- ActiveThreads: " + _aiScheduledThreadPool.getActiveCount(),
  407. " |- getCorePoolSize: " + _aiScheduledThreadPool.getCorePoolSize(),
  408. " |- PoolSize: " + _aiScheduledThreadPool.getPoolSize(),
  409. " |- MaximumPoolSize: " + _aiScheduledThreadPool.getMaximumPoolSize(),
  410. " |- CompletedTasks: " + _aiScheduledThreadPool.getCompletedTaskCount(),
  411. " |- ScheduledTasks: " + _aiScheduledThreadPool.getQueue().size(),
  412. " | -------",
  413. " + Event:",
  414. " |- ActiveThreads: " + _eventScheduledThreadPool.getActiveCount(),
  415. " |- getCorePoolSize: " + _eventScheduledThreadPool.getCorePoolSize(),
  416. " |- PoolSize: " + _eventScheduledThreadPool.getPoolSize(),
  417. " |- MaximumPoolSize: " + _eventScheduledThreadPool.getMaximumPoolSize(),
  418. " |- CompletedTasks: " + _eventScheduledThreadPool.getCompletedTaskCount(),
  419. " |- ScheduledTasks: " + _eventScheduledThreadPool.getQueue().size(),
  420. "TP:",
  421. " + Packets:",
  422. " |- ActiveThreads: " + _generalPacketsThreadPool.getActiveCount(),
  423. " |- getCorePoolSize: " + _generalPacketsThreadPool.getCorePoolSize(),
  424. " |- MaximumPoolSize: " + _generalPacketsThreadPool.getMaximumPoolSize(),
  425. " |- LargestPoolSize: " + _generalPacketsThreadPool.getLargestPoolSize(),
  426. " |- PoolSize: " + _generalPacketsThreadPool.getPoolSize(),
  427. " |- CompletedTasks: " + _generalPacketsThreadPool.getCompletedTaskCount(),
  428. " |- QueuedTasks: " + _generalPacketsThreadPool.getQueue().size(),
  429. " | -------",
  430. " + I/O Packets:",
  431. " |- ActiveThreads: " + _ioPacketsThreadPool.getActiveCount(),
  432. " |- getCorePoolSize: " + _ioPacketsThreadPool.getCorePoolSize(),
  433. " |- MaximumPoolSize: " + _ioPacketsThreadPool.getMaximumPoolSize(),
  434. " |- LargestPoolSize: " + _ioPacketsThreadPool.getLargestPoolSize(),
  435. " |- PoolSize: " + _ioPacketsThreadPool.getPoolSize(),
  436. " |- CompletedTasks: " + _ioPacketsThreadPool.getCompletedTaskCount(),
  437. " |- QueuedTasks: " + _ioPacketsThreadPool.getQueue().size(),
  438. " | -------",
  439. " + General Tasks:",
  440. " |- ActiveThreads: " + _generalThreadPool.getActiveCount(),
  441. " |- getCorePoolSize: " + _generalThreadPool.getCorePoolSize(),
  442. " |- MaximumPoolSize: " + _generalThreadPool.getMaximumPoolSize(),
  443. " |- LargestPoolSize: " + _generalThreadPool.getLargestPoolSize(),
  444. " |- PoolSize: " + _generalThreadPool.getPoolSize(),
  445. " |- CompletedTasks: " + _generalThreadPool.getCompletedTaskCount(),
  446. " |- QueuedTasks: " + _generalThreadPool.getQueue().size(),
  447. " | -------",
  448. " + Event Tasks:",
  449. " |- ActiveThreads: " + _eventThreadPool.getActiveCount(),
  450. " |- getCorePoolSize: " + _eventThreadPool.getCorePoolSize(),
  451. " |- MaximumPoolSize: " + _eventThreadPool.getMaximumPoolSize(),
  452. " |- LargestPoolSize: " + _eventThreadPool.getLargestPoolSize(),
  453. " |- PoolSize: " + _eventThreadPool.getPoolSize(),
  454. " |- CompletedTasks: " + _eventThreadPool.getCompletedTaskCount(),
  455. " |- QueuedTasks: " + _eventThreadPool.getQueue().size(),
  456. " | -------",
  457. " + Javolution stats:",
  458. " |- FastList: " + FastList.report(),
  459. " |- FastMap: " + FastMap.report(),
  460. " |- FastSet: " + FastSet.report(),
  461. " | -------"
  462. };
  463. }
  464. private static class PriorityThreadFactory implements ThreadFactory
  465. {
  466. private final int _prio;
  467. private final String _name;
  468. private final AtomicInteger _threadNumber = new AtomicInteger(1);
  469. private final ThreadGroup _group;
  470. public PriorityThreadFactory(String name, int prio)
  471. {
  472. _prio = prio;
  473. _name = name;
  474. _group = new ThreadGroup(_name);
  475. }
  476. @Override
  477. public Thread newThread(Runnable r)
  478. {
  479. Thread t = new Thread(_group, r, _name + "-" + _threadNumber.getAndIncrement());
  480. t.setPriority(_prio);
  481. return t;
  482. }
  483. public ThreadGroup getGroup()
  484. {
  485. return _group;
  486. }
  487. }
  488. public void shutdown()
  489. {
  490. _shutdown = true;
  491. try
  492. {
  493. _effectsScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  494. _generalScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  495. _generalPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  496. _ioPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  497. _generalThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  498. _eventThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  499. _effectsScheduledThreadPool.shutdown();
  500. _generalScheduledThreadPool.shutdown();
  501. _generalPacketsThreadPool.shutdown();
  502. _ioPacketsThreadPool.shutdown();
  503. _generalThreadPool.shutdown();
  504. _eventThreadPool.shutdown();
  505. _log.info("All ThreadPools are now stopped");
  506. }
  507. catch (InterruptedException e)
  508. {
  509. _log.log(Level.WARNING, "", e);
  510. }
  511. }
  512. public boolean isShutdown()
  513. {
  514. return _shutdown;
  515. }
  516. public void purge()
  517. {
  518. _effectsScheduledThreadPool.purge();
  519. _generalScheduledThreadPool.purge();
  520. _aiScheduledThreadPool.purge();
  521. _eventScheduledThreadPool.purge();
  522. _ioPacketsThreadPool.purge();
  523. _generalPacketsThreadPool.purge();
  524. _generalThreadPool.purge();
  525. _eventThreadPool.purge();
  526. }
  527. public String getPacketStats()
  528. {
  529. final StringBuilder sb = new StringBuilder(1000);
  530. ThreadFactory tf = _generalPacketsThreadPool.getThreadFactory();
  531. if (tf instanceof PriorityThreadFactory)
  532. {
  533. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  534. int count = ptf.getGroup().activeCount();
  535. Thread[] threads = new Thread[count + 2];
  536. ptf.getGroup().enumerate(threads);
  537. 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);
  538. for (Thread t : threads)
  539. {
  540. if (t == null)
  541. {
  542. continue;
  543. }
  544. StringUtil.append(sb, t.getName(), Config.EOL);
  545. for (StackTraceElement ste : t.getStackTrace())
  546. {
  547. StringUtil.append(sb, ste.toString(), Config.EOL);
  548. }
  549. }
  550. }
  551. sb.append("Packet Tp stack traces printed.");
  552. sb.append(Config.EOL);
  553. return sb.toString();
  554. }
  555. public String getIOPacketStats()
  556. {
  557. final StringBuilder sb = new StringBuilder(1000);
  558. ThreadFactory tf = _ioPacketsThreadPool.getThreadFactory();
  559. if (tf instanceof PriorityThreadFactory)
  560. {
  561. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  562. int count = ptf.getGroup().activeCount();
  563. Thread[] threads = new Thread[count + 2];
  564. ptf.getGroup().enumerate(threads);
  565. 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);
  566. for (Thread t : threads)
  567. {
  568. if (t == null)
  569. {
  570. continue;
  571. }
  572. StringUtil.append(sb, t.getName(), Config.EOL);
  573. for (StackTraceElement ste : t.getStackTrace())
  574. {
  575. StringUtil.append(sb, ste.toString(), Config.EOL);
  576. }
  577. }
  578. }
  579. sb.append("Packet Tp stack traces printed." + Config.EOL);
  580. return sb.toString();
  581. }
  582. public String getGeneralStats()
  583. {
  584. final StringBuilder sb = new StringBuilder(1000);
  585. ThreadFactory tf = _generalThreadPool.getThreadFactory();
  586. if (tf instanceof PriorityThreadFactory)
  587. {
  588. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  589. int count = ptf.getGroup().activeCount();
  590. Thread[] threads = new Thread[count + 2];
  591. ptf.getGroup().enumerate(threads);
  592. 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);
  593. for (Thread t : threads)
  594. {
  595. if (t == null)
  596. {
  597. continue;
  598. }
  599. StringUtil.append(sb, t.getName(), Config.EOL);
  600. for (StackTraceElement ste : t.getStackTrace())
  601. {
  602. StringUtil.append(sb, ste.toString(), Config.EOL);
  603. }
  604. }
  605. }
  606. sb.append("Packet Tp stack traces printed." + Config.EOL);
  607. return sb.toString();
  608. }
  609. protected class PurgeTask implements Runnable
  610. {
  611. @Override
  612. public void run()
  613. {
  614. _effectsScheduledThreadPool.purge();
  615. _generalScheduledThreadPool.purge();
  616. _aiScheduledThreadPool.purge();
  617. _eventScheduledThreadPool.purge();
  618. }
  619. }
  620. private static class SingletonHolder
  621. {
  622. protected static final ThreadPoolManager _instance = new ThreadPoolManager();
  623. }
  624. }