From 2b5c29815e606542d1e83574b7e786f11cde2785 Mon Sep 17 00:00:00 2001 From: MobiusDevelopment <8391001+MobiusDevelopment@users.noreply.github.com> Date: Fri, 17 May 2019 01:49:37 +0000 Subject: [PATCH] Evenly distributed tasks among scheduled threadpools. --- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- .../commons/concurrent/ThreadPool.java | 32 +++++++++++++++++-- 14 files changed, 420 insertions(+), 28 deletions(-) diff --git a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_1.0_Ertheia/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_2.5_Underground/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_2.5_Underground/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_3.0_Helios/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_3.0_Helios/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_4.0_GrandCrusade/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_5.0_Salvation/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_5.5_EtinasFate/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_6.0_Fafurion/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_C6_Interlude/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_C6_Interlude/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/commons/concurrent/ThreadPool.java index c70afed3a1..387d50cefb 100644 --- a/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_CT_2.6_HighFive/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/commons/concurrent/ThreadPool.java index 1eae3538ad..9a01778430 100644 --- a/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_Classic_2.0_Saviors/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/commons/concurrent/ThreadPool.java index c70afed3a1..387d50cefb 100644 --- a/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_Classic_2.1_Zaken/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/commons/concurrent/ThreadPool.java index c70afed3a1..387d50cefb 100644 --- a/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_Classic_2.2_Antharas/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/commons/concurrent/ThreadPool.java index c70afed3a1..387d50cefb 100644 --- a/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_Classic_2.3_SevenSigns/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) { diff --git a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/commons/concurrent/ThreadPool.java b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/commons/concurrent/ThreadPool.java index c70afed3a1..387d50cefb 100644 --- a/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/commons/concurrent/ThreadPool.java +++ b/L2J_Mobius_Classic_2.4_SecretOfEmpire/java/org/l2jmobius/commons/concurrent/ThreadPool.java @@ -94,6 +94,34 @@ public final class ThreadPool } } + /** + * Finds the least used ScheduledThreadPoolExecutor from SCHEDULED_POOLS. + * @return the least used ScheduledThreadPoolExecutor. + */ + private static ScheduledThreadPoolExecutor getLeastUsedScheduledThreadPool() + { + int lastQueueSize = Integer.MAX_VALUE; + ScheduledThreadPoolExecutor leastUsed = null; + for (ScheduledThreadPoolExecutor threadPool : SCHEDULED_POOLS) + { + final int queueSize = threadPool.getQueue().size(); + if (lastQueueSize > queueSize) + { + lastQueueSize = queueSize; + leastUsed = threadPool; + } + } + + // Not likely to happen. + if (leastUsed == null) + { + LOGGER.warning("ThreadPool: All threadpool queues reached 2147483647 size! Consider restarting the server!"); + leastUsed = SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT]; + } + + return leastUsed; + } + /** * Creates and executes a one-shot action that becomes enabled after the given delay. * @param runnable : the task to execute. @@ -104,7 +132,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().schedule(new RunnableWrapper(runnable), delay, TimeUnit.MILLISECONDS); } catch (Exception e) { @@ -123,7 +151,7 @@ public final class ThreadPool { try { - return SCHEDULED_POOLS[SCHEDULED_THREAD_RANDOMIZER++ % Config.SCHEDULED_THREAD_POOL_COUNT].scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); + return getLeastUsedScheduledThreadPool().scheduleAtFixedRate(new RunnableWrapper(runnable), initialDelay, period, TimeUnit.MILLISECONDS); } catch (Exception e) {