ThreadPoolManager.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * Copyright (C) 2004-2015 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 org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import com.l2jserver.Config;
  32. import com.l2jserver.util.StringUtil;
  33. /**
  34. * <p>
  35. * This class is made to handle all the ThreadPools used in L2J.
  36. * </p>
  37. * <p>
  38. * 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
  39. * for everything else that needs to be scheduled.<br>
  40. * There also is an {@link #_aiScheduledThreadPool "ai"} {@link ScheduledThreadPoolExecutor ScheduledThreadPool} used for AI Tasks.
  41. * </p>
  42. * <p>
  43. * Tasks can be sent to {@link ScheduledThreadPoolExecutor ScheduledThreadPool} either with:
  44. * <ul>
  45. * <li>{@link #scheduleEffect(Runnable, long, TimeUnit)} and {@link #scheduleEffect(Runnable, long)} : for effects Tasks that needs to be executed only once.</li>
  46. * <li>{@link #scheduleGeneral(Runnable, long, TimeUnit)} and {@link #scheduleGeneral(Runnable, long)} : for scheduled Tasks that needs to be executed once.</li>
  47. * <li>{@link #scheduleAi(Runnable, long, TimeUnit)} and {@link #scheduleAi(Runnable, long)} : for AI Tasks that needs to be executed once</li>
  48. * </ul>
  49. * or
  50. * <ul>
  51. * <li>{@link #scheduleEffectAtFixedRate(Runnable, long, long, TimeUnit)} and {@link #scheduleEffectAtFixedRate(Runnable, long, long)} : for effects Tasks that needs to be executed periodically.</li>
  52. * <li>{@link #scheduleGeneralAtFixedRate(Runnable, long, long, TimeUnit)} and {@link #scheduleGeneralAtFixedRate(Runnable, long, long)} : for scheduled Tasks that needs to be executed periodically.</li>
  53. * <li>{@link #scheduleAiAtFixedRate(Runnable, long, long, TimeUnit)} and {@link #scheduleAiAtFixedRate(Runnable, long, long)} : for AI Tasks that needs to be executed periodically</li>
  54. * </ul>
  55. * </p>
  56. * <p>
  57. * 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.:
  58. * <ul>
  59. * <li>{@link #_generalPacketsThreadPool GeneralPackets} where most packets handler are executed.</li>
  60. * <li>{@link #_ioPacketsThreadPool I/O Packets} where all the i/o packets are executed.</li>
  61. * <li>There will be an AI ThreadPool where AI events should be executed</li>
  62. * <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>
  63. * </ul>
  64. * </p>
  65. * @author -Wooden-
  66. */
  67. public class ThreadPoolManager
  68. {
  69. protected static final Logger _log = LoggerFactory.getLogger(ThreadPoolManager.class);
  70. private static final class RunnableWrapper implements Runnable
  71. {
  72. private final Runnable _r;
  73. public RunnableWrapper(final Runnable r)
  74. {
  75. _r = r;
  76. }
  77. @Override
  78. public final void run()
  79. {
  80. try
  81. {
  82. _r.run();
  83. }
  84. catch (final Throwable e)
  85. {
  86. final Thread t = Thread.currentThread();
  87. final UncaughtExceptionHandler h = t.getUncaughtExceptionHandler();
  88. if (h != null)
  89. {
  90. h.uncaughtException(t, e);
  91. }
  92. }
  93. }
  94. }
  95. protected ScheduledThreadPoolExecutor _effectsScheduledThreadPool;
  96. protected ScheduledThreadPoolExecutor _generalScheduledThreadPool;
  97. protected ScheduledThreadPoolExecutor _aiScheduledThreadPool;
  98. protected ScheduledThreadPoolExecutor _eventScheduledThreadPool;
  99. private final ThreadPoolExecutor _generalPacketsThreadPool;
  100. private final ThreadPoolExecutor _ioPacketsThreadPool;
  101. private final ThreadPoolExecutor _generalThreadPool;
  102. private final ThreadPoolExecutor _eventThreadPool;
  103. private boolean _shutdown;
  104. public static ThreadPoolManager getInstance()
  105. {
  106. return SingletonHolder._instance;
  107. }
  108. protected ThreadPoolManager()
  109. {
  110. _effectsScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.THREAD_P_EFFECTS, new PriorityThreadFactory("EffectsSTPool", Thread.NORM_PRIORITY));
  111. _generalScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.THREAD_P_GENERAL, new PriorityThreadFactory("GeneralSTPool", Thread.NORM_PRIORITY));
  112. _eventScheduledThreadPool = new ScheduledThreadPoolExecutor(Config.THREAD_E_EVENTS, new PriorityThreadFactory("EventSTPool", 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. _eventThreadPool = new ThreadPoolExecutor(Config.EVENT_MAX_THREAD, Config.EVENT_MAX_THREAD + 2, 5L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new PriorityThreadFactory("Event Pool", Thread.NORM_PRIORITY));
  118. scheduleGeneralAtFixedRate(new PurgeTask(), 10, 5, TimeUnit.MINUTES);
  119. }
  120. /**
  121. * Schedules an effect task to be executed after the given delay.
  122. * @param task the task to execute
  123. * @param delay the delay in the given time unit
  124. * @param unit the time unit of the delay parameter
  125. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  126. */
  127. public ScheduledFuture<?> scheduleEffect(Runnable task, long delay, TimeUnit unit)
  128. {
  129. try
  130. {
  131. return _effectsScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  132. }
  133. catch (RejectedExecutionException e)
  134. {
  135. return null;
  136. }
  137. }
  138. /**
  139. * Schedules an effect task to be executed after the given delay.
  140. * @param task the task to execute
  141. * @param delay the delay in milliseconds
  142. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  143. */
  144. public ScheduledFuture<?> scheduleEffect(Runnable task, long delay)
  145. {
  146. return scheduleEffect(task, delay, TimeUnit.MILLISECONDS);
  147. }
  148. /**
  149. * Schedules an effect task to be executed at fixed rate.
  150. * @param task the task to execute
  151. * @param initialDelay the initial delay in the given time unit
  152. * @param period the period between executions in the given time unit
  153. * @param unit the time unit of the initialDelay and period parameters
  154. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  155. */
  156. public ScheduledFuture<?> scheduleEffectAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  157. {
  158. try
  159. {
  160. return _effectsScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  161. }
  162. catch (RejectedExecutionException e)
  163. {
  164. return null; /* shutdown, ignore */
  165. }
  166. }
  167. /**
  168. * Schedules an effect task to be executed at fixed rate.
  169. * @param task the task to execute
  170. * @param initialDelay the initial delay in milliseconds
  171. * @param period the period between executions in milliseconds
  172. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  173. */
  174. public ScheduledFuture<?> scheduleEffectAtFixedRate(Runnable task, long initialDelay, long period)
  175. {
  176. return scheduleEffectAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  177. }
  178. /**
  179. * Schedules a general task to be executed after the given delay.
  180. * @param task the task to execute
  181. * @param delay the delay in the given time unit
  182. * @param unit the time unit of the delay parameter
  183. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  184. */
  185. public ScheduledFuture<?> scheduleGeneral(Runnable task, long delay, TimeUnit unit)
  186. {
  187. try
  188. {
  189. return _generalScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  190. }
  191. catch (RejectedExecutionException e)
  192. {
  193. return null; /* shutdown, ignore */
  194. }
  195. }
  196. /**
  197. * Schedules a general task to be executed after the given delay.
  198. * @param task the task to execute
  199. * @param delay the delay in milliseconds
  200. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  201. */
  202. public ScheduledFuture<?> scheduleGeneral(Runnable task, long delay)
  203. {
  204. return scheduleGeneral(task, delay, TimeUnit.MILLISECONDS);
  205. }
  206. /**
  207. * Schedules a general task to be executed at fixed rate.
  208. * @param task the task to execute
  209. * @param initialDelay the initial delay in the given time unit
  210. * @param period the period between executions in the given time unit
  211. * @param unit the time unit of the initialDelay and period parameters
  212. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  213. */
  214. public ScheduledFuture<?> scheduleGeneralAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  215. {
  216. try
  217. {
  218. return _generalScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  219. }
  220. catch (RejectedExecutionException e)
  221. {
  222. return null; /* shutdown, ignore */
  223. }
  224. }
  225. /**
  226. * Schedules a event task to be executed after the given delay.
  227. * @param task the task to execute
  228. * @param delay the delay in the given time unit
  229. * @param unit the time unit of the delay parameter
  230. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  231. */
  232. public ScheduledFuture<?> scheduleEvent(Runnable task, long delay, TimeUnit unit)
  233. {
  234. try
  235. {
  236. return _eventScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  237. }
  238. catch (RejectedExecutionException e)
  239. {
  240. return null; /* shutdown, ignore */
  241. }
  242. }
  243. /**
  244. * Schedules a event task to be executed after the given delay.
  245. * @param task the task to execute
  246. * @param delay the delay in milliseconds
  247. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  248. */
  249. public ScheduledFuture<?> scheduleEvent(Runnable task, long delay)
  250. {
  251. return scheduleEvent(task, delay, TimeUnit.MILLISECONDS);
  252. }
  253. /**
  254. * Schedules a event task to be executed at fixed rate.
  255. * @param task the task to execute
  256. * @param initialDelay the initial delay in the given time unit
  257. * @param period the period between executions in the given time unit
  258. * @param unit the time unit of the initialDelay and period parameters
  259. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  260. */
  261. public ScheduledFuture<?> scheduleEventAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  262. {
  263. try
  264. {
  265. return _eventScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  266. }
  267. catch (RejectedExecutionException e)
  268. {
  269. return null; /* shutdown, ignore */
  270. }
  271. }
  272. /**
  273. * Schedules a general task to be executed at fixed rate.
  274. * @param task the task to execute
  275. * @param initialDelay the initial delay in milliseconds
  276. * @param period the period between executions in milliseconds
  277. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  278. */
  279. public ScheduledFuture<?> scheduleGeneralAtFixedRate(Runnable task, long initialDelay, long period)
  280. {
  281. return scheduleGeneralAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  282. }
  283. /**
  284. * Schedules an AI task to be executed after the given delay.
  285. * @param task the task to execute
  286. * @param delay the delay in the given time unit
  287. * @param unit the time unit of the delay parameter
  288. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  289. */
  290. public ScheduledFuture<?> scheduleAi(Runnable task, long delay, TimeUnit unit)
  291. {
  292. try
  293. {
  294. return _aiScheduledThreadPool.schedule(new RunnableWrapper(task), delay, unit);
  295. }
  296. catch (RejectedExecutionException e)
  297. {
  298. return null; /* shutdown, ignore */
  299. }
  300. }
  301. /**
  302. * Schedules an AI task to be executed after the given delay.
  303. * @param task the task to execute
  304. * @param delay the delay in milliseconds
  305. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  306. */
  307. public ScheduledFuture<?> scheduleAi(Runnable task, long delay)
  308. {
  309. return scheduleAi(task, delay, TimeUnit.MILLISECONDS);
  310. }
  311. /**
  312. * Schedules a general task to be executed at fixed rate.
  313. * @param task the task to execute
  314. * @param initialDelay the initial delay in the given time unit
  315. * @param period the period between executions in the given time unit
  316. * @param unit the time unit of the initialDelay and period parameters
  317. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  318. */
  319. public ScheduledFuture<?> scheduleAiAtFixedRate(Runnable task, long initialDelay, long period, TimeUnit unit)
  320. {
  321. try
  322. {
  323. return _aiScheduledThreadPool.scheduleAtFixedRate(new RunnableWrapper(task), initialDelay, period, unit);
  324. }
  325. catch (RejectedExecutionException e)
  326. {
  327. return null; /* shutdown, ignore */
  328. }
  329. }
  330. /**
  331. * Schedules a general task to be executed at fixed rate.
  332. * @param task the task to execute
  333. * @param initialDelay the initial delay in milliseconds
  334. * @param period the period between executions in milliseconds
  335. * @return a ScheduledFuture representing pending completion of the task, and whose get() method will throw an exception upon cancellation
  336. */
  337. public ScheduledFuture<?> scheduleAiAtFixedRate(Runnable task, long initialDelay, long period)
  338. {
  339. return scheduleAiAtFixedRate(task, initialDelay, period, TimeUnit.MILLISECONDS);
  340. }
  341. /**
  342. * Executes a packet task sometime in future in another thread.
  343. * @param task the task to execute
  344. */
  345. public void executePacket(Runnable task)
  346. {
  347. try
  348. {
  349. _generalPacketsThreadPool.execute(task);
  350. }
  351. catch (RejectedExecutionException e)
  352. {
  353. /* shutdown, ignore */
  354. }
  355. }
  356. /**
  357. * Executes an IO packet task sometime in future in another thread.
  358. * @param task the task to execute
  359. */
  360. public void executeIOPacket(Runnable task)
  361. {
  362. try
  363. {
  364. _ioPacketsThreadPool.execute(task);
  365. }
  366. catch (RejectedExecutionException e)
  367. {
  368. /* shutdown, ignore */
  369. }
  370. }
  371. /**
  372. * Executes a general task sometime in future in another thread.
  373. * @param task the task to execute
  374. */
  375. public void executeGeneral(Runnable task)
  376. {
  377. try
  378. {
  379. _generalThreadPool.execute(new RunnableWrapper(task));
  380. }
  381. catch (RejectedExecutionException e)
  382. {
  383. /* shutdown, ignore */
  384. }
  385. }
  386. /**
  387. * Executes an AI task sometime in future in another thread.
  388. * @param task the task to execute
  389. */
  390. public void executeAi(Runnable task)
  391. {
  392. try
  393. {
  394. _aiScheduledThreadPool.execute(new RunnableWrapper(task));
  395. }
  396. catch (RejectedExecutionException e)
  397. {
  398. /* shutdown, ignore */
  399. }
  400. }
  401. /**
  402. * Executes an Event task sometime in future in another thread.
  403. * @param task the task to execute
  404. */
  405. public void executeEvent(Runnable task)
  406. {
  407. try
  408. {
  409. _eventThreadPool.execute(new RunnableWrapper(task));
  410. }
  411. catch (RejectedExecutionException e)
  412. {
  413. /* shutdown, ignore */
  414. }
  415. }
  416. public String[] getStats()
  417. {
  418. return new String[]
  419. {
  420. "STP:",
  421. " + Effects:",
  422. " |- ActiveThreads: " + _effectsScheduledThreadPool.getActiveCount(),
  423. " |- getCorePoolSize: " + _effectsScheduledThreadPool.getCorePoolSize(),
  424. " |- PoolSize: " + _effectsScheduledThreadPool.getPoolSize(),
  425. " |- MaximumPoolSize: " + _effectsScheduledThreadPool.getMaximumPoolSize(),
  426. " |- CompletedTasks: " + _effectsScheduledThreadPool.getCompletedTaskCount(),
  427. " |- ScheduledTasks: " + _effectsScheduledThreadPool.getQueue().size(),
  428. " | -------",
  429. " + General:",
  430. " |- ActiveThreads: " + _generalScheduledThreadPool.getActiveCount(),
  431. " |- getCorePoolSize: " + _generalScheduledThreadPool.getCorePoolSize(),
  432. " |- PoolSize: " + _generalScheduledThreadPool.getPoolSize(),
  433. " |- MaximumPoolSize: " + _generalScheduledThreadPool.getMaximumPoolSize(),
  434. " |- CompletedTasks: " + _generalScheduledThreadPool.getCompletedTaskCount(),
  435. " |- ScheduledTasks: " + _generalScheduledThreadPool.getQueue().size(),
  436. " | -------",
  437. " + AI:",
  438. " |- ActiveThreads: " + _aiScheduledThreadPool.getActiveCount(),
  439. " |- getCorePoolSize: " + _aiScheduledThreadPool.getCorePoolSize(),
  440. " |- PoolSize: " + _aiScheduledThreadPool.getPoolSize(),
  441. " |- MaximumPoolSize: " + _aiScheduledThreadPool.getMaximumPoolSize(),
  442. " |- CompletedTasks: " + _aiScheduledThreadPool.getCompletedTaskCount(),
  443. " |- ScheduledTasks: " + _aiScheduledThreadPool.getQueue().size(),
  444. " | -------",
  445. " + Event:",
  446. " |- ActiveThreads: " + _eventScheduledThreadPool.getActiveCount(),
  447. " |- getCorePoolSize: " + _eventScheduledThreadPool.getCorePoolSize(),
  448. " |- PoolSize: " + _eventScheduledThreadPool.getPoolSize(),
  449. " |- MaximumPoolSize: " + _eventScheduledThreadPool.getMaximumPoolSize(),
  450. " |- CompletedTasks: " + _eventScheduledThreadPool.getCompletedTaskCount(),
  451. " |- ScheduledTasks: " + _eventScheduledThreadPool.getQueue().size(),
  452. "TP:",
  453. " + Packets:",
  454. " |- ActiveThreads: " + _generalPacketsThreadPool.getActiveCount(),
  455. " |- getCorePoolSize: " + _generalPacketsThreadPool.getCorePoolSize(),
  456. " |- MaximumPoolSize: " + _generalPacketsThreadPool.getMaximumPoolSize(),
  457. " |- LargestPoolSize: " + _generalPacketsThreadPool.getLargestPoolSize(),
  458. " |- PoolSize: " + _generalPacketsThreadPool.getPoolSize(),
  459. " |- CompletedTasks: " + _generalPacketsThreadPool.getCompletedTaskCount(),
  460. " |- QueuedTasks: " + _generalPacketsThreadPool.getQueue().size(),
  461. " | -------",
  462. " + I/O Packets:",
  463. " |- ActiveThreads: " + _ioPacketsThreadPool.getActiveCount(),
  464. " |- getCorePoolSize: " + _ioPacketsThreadPool.getCorePoolSize(),
  465. " |- MaximumPoolSize: " + _ioPacketsThreadPool.getMaximumPoolSize(),
  466. " |- LargestPoolSize: " + _ioPacketsThreadPool.getLargestPoolSize(),
  467. " |- PoolSize: " + _ioPacketsThreadPool.getPoolSize(),
  468. " |- CompletedTasks: " + _ioPacketsThreadPool.getCompletedTaskCount(),
  469. " |- QueuedTasks: " + _ioPacketsThreadPool.getQueue().size(),
  470. " | -------",
  471. " + General Tasks:",
  472. " |- ActiveThreads: " + _generalThreadPool.getActiveCount(),
  473. " |- getCorePoolSize: " + _generalThreadPool.getCorePoolSize(),
  474. " |- MaximumPoolSize: " + _generalThreadPool.getMaximumPoolSize(),
  475. " |- LargestPoolSize: " + _generalThreadPool.getLargestPoolSize(),
  476. " |- PoolSize: " + _generalThreadPool.getPoolSize(),
  477. " |- CompletedTasks: " + _generalThreadPool.getCompletedTaskCount(),
  478. " |- QueuedTasks: " + _generalThreadPool.getQueue().size(),
  479. " | -------",
  480. " + Event Tasks:",
  481. " |- ActiveThreads: " + _eventThreadPool.getActiveCount(),
  482. " |- getCorePoolSize: " + _eventThreadPool.getCorePoolSize(),
  483. " |- MaximumPoolSize: " + _eventThreadPool.getMaximumPoolSize(),
  484. " |- LargestPoolSize: " + _eventThreadPool.getLargestPoolSize(),
  485. " |- PoolSize: " + _eventThreadPool.getPoolSize(),
  486. " |- CompletedTasks: " + _eventThreadPool.getCompletedTaskCount(),
  487. " |- QueuedTasks: " + _eventThreadPool.getQueue().size(),
  488. " | -------"
  489. };
  490. }
  491. private static class PriorityThreadFactory implements ThreadFactory
  492. {
  493. private final int _prio;
  494. private final String _name;
  495. private final AtomicInteger _threadNumber = new AtomicInteger(1);
  496. private final ThreadGroup _group;
  497. public PriorityThreadFactory(String name, int prio)
  498. {
  499. _prio = prio;
  500. _name = name;
  501. _group = new ThreadGroup(_name);
  502. }
  503. @Override
  504. public Thread newThread(Runnable r)
  505. {
  506. Thread t = new Thread(_group, r, _name + "-" + _threadNumber.getAndIncrement());
  507. t.setPriority(_prio);
  508. return t;
  509. }
  510. public ThreadGroup getGroup()
  511. {
  512. return _group;
  513. }
  514. }
  515. public void shutdown()
  516. {
  517. _shutdown = true;
  518. try
  519. {
  520. _effectsScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  521. _generalScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  522. _generalPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  523. _ioPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  524. _generalThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  525. _eventThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  526. _effectsScheduledThreadPool.shutdown();
  527. _generalScheduledThreadPool.shutdown();
  528. _generalPacketsThreadPool.shutdown();
  529. _ioPacketsThreadPool.shutdown();
  530. _generalThreadPool.shutdown();
  531. _eventThreadPool.shutdown();
  532. _log.info("All ThreadPools are now stopped");
  533. }
  534. catch (InterruptedException e)
  535. {
  536. _log.warn("There has been a problem shuting down the thread pool manager!", e);
  537. }
  538. }
  539. public boolean isShutdown()
  540. {
  541. return _shutdown;
  542. }
  543. public void purge()
  544. {
  545. _effectsScheduledThreadPool.purge();
  546. _generalScheduledThreadPool.purge();
  547. _aiScheduledThreadPool.purge();
  548. _eventScheduledThreadPool.purge();
  549. _ioPacketsThreadPool.purge();
  550. _generalPacketsThreadPool.purge();
  551. _generalThreadPool.purge();
  552. _eventThreadPool.purge();
  553. }
  554. public String getPacketStats()
  555. {
  556. final StringBuilder sb = new StringBuilder(1000);
  557. ThreadFactory tf = _generalPacketsThreadPool.getThreadFactory();
  558. if (tf instanceof PriorityThreadFactory)
  559. {
  560. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  561. int count = ptf.getGroup().activeCount();
  562. Thread[] threads = new Thread[count + 2];
  563. ptf.getGroup().enumerate(threads);
  564. 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);
  565. for (Thread t : threads)
  566. {
  567. if (t == null)
  568. {
  569. continue;
  570. }
  571. StringUtil.append(sb, t.getName(), Config.EOL);
  572. for (StackTraceElement ste : t.getStackTrace())
  573. {
  574. StringUtil.append(sb, ste.toString(), Config.EOL);
  575. }
  576. }
  577. }
  578. sb.append("Packet Tp stack traces printed.");
  579. sb.append(Config.EOL);
  580. return sb.toString();
  581. }
  582. public String getIOPacketStats()
  583. {
  584. final StringBuilder sb = new StringBuilder(1000);
  585. ThreadFactory tf = _ioPacketsThreadPool.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, "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);
  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. public String getGeneralStats()
  610. {
  611. final StringBuilder sb = new StringBuilder(1000);
  612. ThreadFactory tf = _generalThreadPool.getThreadFactory();
  613. if (tf instanceof PriorityThreadFactory)
  614. {
  615. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  616. int count = ptf.getGroup().activeCount();
  617. Thread[] threads = new Thread[count + 2];
  618. ptf.getGroup().enumerate(threads);
  619. 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);
  620. for (Thread t : threads)
  621. {
  622. if (t == null)
  623. {
  624. continue;
  625. }
  626. StringUtil.append(sb, t.getName(), Config.EOL);
  627. for (StackTraceElement ste : t.getStackTrace())
  628. {
  629. StringUtil.append(sb, ste.toString(), Config.EOL);
  630. }
  631. }
  632. }
  633. sb.append("Packet Tp stack traces printed." + Config.EOL);
  634. return sb.toString();
  635. }
  636. protected class PurgeTask implements Runnable
  637. {
  638. @Override
  639. public void run()
  640. {
  641. _effectsScheduledThreadPool.purge();
  642. _generalScheduledThreadPool.purge();
  643. _aiScheduledThreadPool.purge();
  644. _eventScheduledThreadPool.purge();
  645. }
  646. }
  647. private static class SingletonHolder
  648. {
  649. protected static final ThreadPoolManager _instance = new ThreadPoolManager();
  650. }
  651. }