ThreadPoolManager.java 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. try
  351. {
  352. _generalPacketsThreadPool.execute(task);
  353. }
  354. catch (RejectedExecutionException e)
  355. {
  356. /* shutdown, ignore */
  357. }
  358. }
  359. /**
  360. * Executes an IO packet task sometime in future in another thread.
  361. * @param task the task to execute
  362. */
  363. public void executeIOPacket(Runnable task)
  364. {
  365. try
  366. {
  367. _ioPacketsThreadPool.execute(task);
  368. }
  369. catch (RejectedExecutionException e)
  370. {
  371. /* shutdown, ignore */
  372. }
  373. }
  374. /**
  375. * Executes a general task sometime in future in another thread.
  376. * @param task the task to execute
  377. */
  378. public void executeGeneral(Runnable task)
  379. {
  380. try
  381. {
  382. _generalThreadPool.execute(new RunnableWrapper(task));
  383. }
  384. catch (RejectedExecutionException e)
  385. {
  386. /* shutdown, ignore */
  387. }
  388. }
  389. /**
  390. * Executes an AI task sometime in future in another thread.
  391. * @param task the task to execute
  392. */
  393. public void executeAi(Runnable task)
  394. {
  395. try
  396. {
  397. _aiScheduledThreadPool.execute(new RunnableWrapper(task));
  398. }
  399. catch (RejectedExecutionException e)
  400. {
  401. /* shutdown, ignore */
  402. }
  403. }
  404. /**
  405. * Executes an Event task sometime in future in another thread.
  406. * @param task the task to execute
  407. */
  408. public void executeEvent(Runnable task)
  409. {
  410. try
  411. {
  412. _eventThreadPool.execute(new RunnableWrapper(task));
  413. }
  414. catch (RejectedExecutionException e)
  415. {
  416. /* shutdown, ignore */
  417. }
  418. }
  419. public String[] getStats()
  420. {
  421. return new String[]
  422. {
  423. "STP:",
  424. " + Effects:",
  425. " |- ActiveThreads: " + _effectsScheduledThreadPool.getActiveCount(),
  426. " |- getCorePoolSize: " + _effectsScheduledThreadPool.getCorePoolSize(),
  427. " |- PoolSize: " + _effectsScheduledThreadPool.getPoolSize(),
  428. " |- MaximumPoolSize: " + _effectsScheduledThreadPool.getMaximumPoolSize(),
  429. " |- CompletedTasks: " + _effectsScheduledThreadPool.getCompletedTaskCount(),
  430. " |- ScheduledTasks: " + _effectsScheduledThreadPool.getQueue().size(),
  431. " | -------",
  432. " + General:",
  433. " |- ActiveThreads: " + _generalScheduledThreadPool.getActiveCount(),
  434. " |- getCorePoolSize: " + _generalScheduledThreadPool.getCorePoolSize(),
  435. " |- PoolSize: " + _generalScheduledThreadPool.getPoolSize(),
  436. " |- MaximumPoolSize: " + _generalScheduledThreadPool.getMaximumPoolSize(),
  437. " |- CompletedTasks: " + _generalScheduledThreadPool.getCompletedTaskCount(),
  438. " |- ScheduledTasks: " + _generalScheduledThreadPool.getQueue().size(),
  439. " | -------",
  440. " + AI:",
  441. " |- ActiveThreads: " + _aiScheduledThreadPool.getActiveCount(),
  442. " |- getCorePoolSize: " + _aiScheduledThreadPool.getCorePoolSize(),
  443. " |- PoolSize: " + _aiScheduledThreadPool.getPoolSize(),
  444. " |- MaximumPoolSize: " + _aiScheduledThreadPool.getMaximumPoolSize(),
  445. " |- CompletedTasks: " + _aiScheduledThreadPool.getCompletedTaskCount(),
  446. " |- ScheduledTasks: " + _aiScheduledThreadPool.getQueue().size(),
  447. " | -------",
  448. " + Event:",
  449. " |- ActiveThreads: " + _eventScheduledThreadPool.getActiveCount(),
  450. " |- getCorePoolSize: " + _eventScheduledThreadPool.getCorePoolSize(),
  451. " |- PoolSize: " + _eventScheduledThreadPool.getPoolSize(),
  452. " |- MaximumPoolSize: " + _eventScheduledThreadPool.getMaximumPoolSize(),
  453. " |- CompletedTasks: " + _eventScheduledThreadPool.getCompletedTaskCount(),
  454. " |- ScheduledTasks: " + _eventScheduledThreadPool.getQueue().size(),
  455. "TP:",
  456. " + Packets:",
  457. " |- ActiveThreads: " + _generalPacketsThreadPool.getActiveCount(),
  458. " |- getCorePoolSize: " + _generalPacketsThreadPool.getCorePoolSize(),
  459. " |- MaximumPoolSize: " + _generalPacketsThreadPool.getMaximumPoolSize(),
  460. " |- LargestPoolSize: " + _generalPacketsThreadPool.getLargestPoolSize(),
  461. " |- PoolSize: " + _generalPacketsThreadPool.getPoolSize(),
  462. " |- CompletedTasks: " + _generalPacketsThreadPool.getCompletedTaskCount(),
  463. " |- QueuedTasks: " + _generalPacketsThreadPool.getQueue().size(),
  464. " | -------",
  465. " + I/O Packets:",
  466. " |- ActiveThreads: " + _ioPacketsThreadPool.getActiveCount(),
  467. " |- getCorePoolSize: " + _ioPacketsThreadPool.getCorePoolSize(),
  468. " |- MaximumPoolSize: " + _ioPacketsThreadPool.getMaximumPoolSize(),
  469. " |- LargestPoolSize: " + _ioPacketsThreadPool.getLargestPoolSize(),
  470. " |- PoolSize: " + _ioPacketsThreadPool.getPoolSize(),
  471. " |- CompletedTasks: " + _ioPacketsThreadPool.getCompletedTaskCount(),
  472. " |- QueuedTasks: " + _ioPacketsThreadPool.getQueue().size(),
  473. " | -------",
  474. " + General Tasks:",
  475. " |- ActiveThreads: " + _generalThreadPool.getActiveCount(),
  476. " |- getCorePoolSize: " + _generalThreadPool.getCorePoolSize(),
  477. " |- MaximumPoolSize: " + _generalThreadPool.getMaximumPoolSize(),
  478. " |- LargestPoolSize: " + _generalThreadPool.getLargestPoolSize(),
  479. " |- PoolSize: " + _generalThreadPool.getPoolSize(),
  480. " |- CompletedTasks: " + _generalThreadPool.getCompletedTaskCount(),
  481. " |- QueuedTasks: " + _generalThreadPool.getQueue().size(),
  482. " | -------",
  483. " + Event Tasks:",
  484. " |- ActiveThreads: " + _eventThreadPool.getActiveCount(),
  485. " |- getCorePoolSize: " + _eventThreadPool.getCorePoolSize(),
  486. " |- MaximumPoolSize: " + _eventThreadPool.getMaximumPoolSize(),
  487. " |- LargestPoolSize: " + _eventThreadPool.getLargestPoolSize(),
  488. " |- PoolSize: " + _eventThreadPool.getPoolSize(),
  489. " |- CompletedTasks: " + _eventThreadPool.getCompletedTaskCount(),
  490. " |- QueuedTasks: " + _eventThreadPool.getQueue().size(),
  491. " | -------",
  492. " + Javolution stats:",
  493. " |- FastList: " + FastList.report(),
  494. " |- FastMap: " + FastMap.report(),
  495. " |- FastSet: " + FastSet.report(),
  496. " | -------"
  497. };
  498. }
  499. private static class PriorityThreadFactory implements ThreadFactory
  500. {
  501. private final int _prio;
  502. private final String _name;
  503. private final AtomicInteger _threadNumber = new AtomicInteger(1);
  504. private final ThreadGroup _group;
  505. public PriorityThreadFactory(String name, int prio)
  506. {
  507. _prio = prio;
  508. _name = name;
  509. _group = new ThreadGroup(_name);
  510. }
  511. @Override
  512. public Thread newThread(Runnable r)
  513. {
  514. Thread t = new Thread(_group, r, _name + "-" + _threadNumber.getAndIncrement());
  515. t.setPriority(_prio);
  516. return t;
  517. }
  518. public ThreadGroup getGroup()
  519. {
  520. return _group;
  521. }
  522. }
  523. public void shutdown()
  524. {
  525. _shutdown = true;
  526. try
  527. {
  528. _effectsScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  529. _generalScheduledThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  530. _generalPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  531. _ioPacketsThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  532. _generalThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  533. _eventThreadPool.awaitTermination(1, TimeUnit.SECONDS);
  534. _effectsScheduledThreadPool.shutdown();
  535. _generalScheduledThreadPool.shutdown();
  536. _generalPacketsThreadPool.shutdown();
  537. _ioPacketsThreadPool.shutdown();
  538. _generalThreadPool.shutdown();
  539. _eventThreadPool.shutdown();
  540. _log.info("All ThreadPools are now stopped");
  541. }
  542. catch (InterruptedException e)
  543. {
  544. _log.log(Level.WARNING, "", e);
  545. }
  546. }
  547. public boolean isShutdown()
  548. {
  549. return _shutdown;
  550. }
  551. public void purge()
  552. {
  553. _effectsScheduledThreadPool.purge();
  554. _generalScheduledThreadPool.purge();
  555. _aiScheduledThreadPool.purge();
  556. _eventScheduledThreadPool.purge();
  557. _ioPacketsThreadPool.purge();
  558. _generalPacketsThreadPool.purge();
  559. _generalThreadPool.purge();
  560. _eventThreadPool.purge();
  561. }
  562. public String getPacketStats()
  563. {
  564. final StringBuilder sb = new StringBuilder(1000);
  565. ThreadFactory tf = _generalPacketsThreadPool.getThreadFactory();
  566. if (tf instanceof PriorityThreadFactory)
  567. {
  568. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  569. int count = ptf.getGroup().activeCount();
  570. Thread[] threads = new Thread[count + 2];
  571. ptf.getGroup().enumerate(threads);
  572. 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);
  573. for (Thread t : threads)
  574. {
  575. if (t == null)
  576. {
  577. continue;
  578. }
  579. StringUtil.append(sb, t.getName(), Config.EOL);
  580. for (StackTraceElement ste : t.getStackTrace())
  581. {
  582. StringUtil.append(sb, ste.toString(), Config.EOL);
  583. }
  584. }
  585. }
  586. sb.append("Packet Tp stack traces printed.");
  587. sb.append(Config.EOL);
  588. return sb.toString();
  589. }
  590. public String getIOPacketStats()
  591. {
  592. final StringBuilder sb = new StringBuilder(1000);
  593. ThreadFactory tf = _ioPacketsThreadPool.getThreadFactory();
  594. if (tf instanceof PriorityThreadFactory)
  595. {
  596. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  597. int count = ptf.getGroup().activeCount();
  598. Thread[] threads = new Thread[count + 2];
  599. ptf.getGroup().enumerate(threads);
  600. 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);
  601. for (Thread t : threads)
  602. {
  603. if (t == null)
  604. {
  605. continue;
  606. }
  607. StringUtil.append(sb, t.getName(), Config.EOL);
  608. for (StackTraceElement ste : t.getStackTrace())
  609. {
  610. StringUtil.append(sb, ste.toString(), Config.EOL);
  611. }
  612. }
  613. }
  614. sb.append("Packet Tp stack traces printed." + Config.EOL);
  615. return sb.toString();
  616. }
  617. public String getGeneralStats()
  618. {
  619. final StringBuilder sb = new StringBuilder(1000);
  620. ThreadFactory tf = _generalThreadPool.getThreadFactory();
  621. if (tf instanceof PriorityThreadFactory)
  622. {
  623. PriorityThreadFactory ptf = (PriorityThreadFactory) tf;
  624. int count = ptf.getGroup().activeCount();
  625. Thread[] threads = new Thread[count + 2];
  626. ptf.getGroup().enumerate(threads);
  627. 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);
  628. for (Thread t : threads)
  629. {
  630. if (t == null)
  631. {
  632. continue;
  633. }
  634. StringUtil.append(sb, t.getName(), Config.EOL);
  635. for (StackTraceElement ste : t.getStackTrace())
  636. {
  637. StringUtil.append(sb, ste.toString(), Config.EOL);
  638. }
  639. }
  640. }
  641. sb.append("Packet Tp stack traces printed." + Config.EOL);
  642. return sb.toString();
  643. }
  644. protected class PurgeTask implements Runnable
  645. {
  646. @Override
  647. public void run()
  648. {
  649. _effectsScheduledThreadPool.purge();
  650. _generalScheduledThreadPool.purge();
  651. _aiScheduledThreadPool.purge();
  652. _eventScheduledThreadPool.purge();
  653. }
  654. }
  655. private static class SingletonHolder
  656. {
  657. protected static final ThreadPoolManager _instance = new ThreadPoolManager();
  658. }
  659. }