Stun server transaction: reduce number of threads used for expiration. (#154)
* Reorder fields so final fields set in constructor come first, then mutable state of transaction. * Rename STUN transaction lifetime field. * Eliminate isRetransmitting field which is actually computed property from response != null * Use monotonic clock (System.nanoTime) to check expiration. Replaced synchronized methods with atomic variables. * Re-factored TransactionID implementation. Use less mutable state. Fixed hash function to produce random values, instead of sequentially growing values. * Do no perform full Hashtable lookup to find transaction with specified ID. * Use check before acquiring lock in StunStack. * Implement ScheduledExecutorService based expired transaction collector. * Restored self-cancel of expired server transactions collector when no server transactions at all. * Fixed isExpire check in StunServerTransaction * Fixed collection runnable to remove not only values but also keys from Hashtable. * Perform elapsed check over milliseconds, not nanoseconds. * Do not leave null values in clientTransactions/serverTransactions Hashtables when removing transaction, but leaving it's ID in Hashtable. * Remove condition around cheap logger calls. * Minor formatting fixes. * Log number of non-expired transactions after collection to verity it is not growing over time. * Fixed multi-line string formatting. * Moved construction of ScheduledThreadPoolExecutor to helper method. Adjusted default settings of pool. * Allow to create single threaded pool as well as bounded to number of CPUs on machine pool. * Fixed name collision with same class in other package. * Minor styling changes to reduce diff. * Removed unused import. * Do check of response when it's actually supposed to be non null. * Add actual length to error message. * Allow transaction ID of incorrect size. * Allow constructing TransactionID with invalid length. * Rollback all refactoring other than reducing thread count. * Use default 1 minute timeout for threads in pool. * Fixed typo in method name.
This commit is contained in:
parent
513fb588d6
commit
3239ed12fc
@ -119,21 +119,9 @@ public class Agent
|
|||||||
/**
|
/**
|
||||||
* The ScheduledExecutorService to execute Agent's scheduled tasks
|
* The ScheduledExecutorService to execute Agent's scheduled tasks
|
||||||
*/
|
*/
|
||||||
private static final ScheduledExecutorService agentTasksScheduler;
|
private static final ScheduledExecutorService agentTasksScheduler
|
||||||
|
= ExecutorFactory.createCPUBoundScheduledExecutor(
|
||||||
static
|
"ice4j.Agent-", 60, TimeUnit.SECONDS);
|
||||||
{
|
|
||||||
CustomizableThreadFactory threadFactory
|
|
||||||
= new CustomizableThreadFactory("ice4j.Agent-", true);
|
|
||||||
|
|
||||||
final ScheduledThreadPoolExecutor terminationExecutor
|
|
||||||
= new ScheduledThreadPoolExecutor(0, threadFactory);
|
|
||||||
terminationExecutor.setKeepAliveTime(10, TimeUnit.SECONDS);
|
|
||||||
terminationExecutor.setRemoveOnCancelPolicy(true);
|
|
||||||
agentTasksScheduler
|
|
||||||
= Executors.unconfigurableScheduledExecutorService(
|
|
||||||
terminationExecutor);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Termination task which will be scheduled with timeout
|
* Termination task which will be scheduled with timeout
|
||||||
|
|||||||
@ -77,20 +77,9 @@ public class StunClientTransaction
|
|||||||
* The pool of <tt>Thread</tt>s which retransmit
|
* The pool of <tt>Thread</tt>s which retransmit
|
||||||
* <tt>StunClientTransaction</tt>s.
|
* <tt>StunClientTransaction</tt>s.
|
||||||
*/
|
*/
|
||||||
private static final ScheduledExecutorService retransmissionThreadPool;
|
private static final ScheduledExecutorService retransmissionThreadPool
|
||||||
|
= ExecutorFactory.createCPUBoundScheduledExecutor(
|
||||||
static {
|
"ice4j.StunClientTransaction-", 60, TimeUnit.SECONDS);
|
||||||
CustomizableThreadFactory threadFactory
|
|
||||||
= new CustomizableThreadFactory("ice4j.StunClientTransaction-", true);
|
|
||||||
|
|
||||||
final ScheduledThreadPoolExecutor terminationExecutor
|
|
||||||
= new ScheduledThreadPoolExecutor(0, threadFactory);
|
|
||||||
terminationExecutor.setKeepAliveTime(60, TimeUnit.SECONDS);
|
|
||||||
terminationExecutor.setRemoveOnCancelPolicy(true);
|
|
||||||
retransmissionThreadPool
|
|
||||||
= Executors.unconfigurableScheduledExecutorService(
|
|
||||||
terminationExecutor);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Maximum number of retransmissions. Once this number is reached and if no
|
* Maximum number of retransmissions. Once this number is reached and if no
|
||||||
|
|||||||
@ -21,6 +21,7 @@ import java.io.*;
|
|||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.security.*;
|
import java.security.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
import java.util.concurrent.*;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
|
|
||||||
import javax.crypto.*;
|
import javax.crypto.*;
|
||||||
@ -30,6 +31,7 @@ import org.ice4j.attribute.*;
|
|||||||
import org.ice4j.message.*;
|
import org.ice4j.message.*;
|
||||||
import org.ice4j.security.*;
|
import org.ice4j.security.*;
|
||||||
import org.ice4j.socket.*;
|
import org.ice4j.socket.*;
|
||||||
|
import org.ice4j.util.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The entry point to the Stun4J stack. The class is used to start, stop and
|
* The entry point to the Stun4J stack. The class is used to start, stop and
|
||||||
@ -52,8 +54,8 @@ public class StunStack
|
|||||||
* The <tt>Logger</tt> used by the <tt>StunStack</tt> class and its
|
* The <tt>Logger</tt> used by the <tt>StunStack</tt> class and its
|
||||||
* instances for logging output.
|
* instances for logging output.
|
||||||
*/
|
*/
|
||||||
private static final Logger logger
|
private static final java.util.logging.Logger logger
|
||||||
= Logger.getLogger(StunStack.class.getName());
|
= java.util.logging.Logger.getLogger(StunStack.class.getName());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The indicator which determines whether
|
* The indicator which determines whether
|
||||||
@ -64,6 +66,14 @@ public class StunStack
|
|||||||
*/
|
*/
|
||||||
private static Mac mac;
|
private static Mac mac;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ScheduledExecutorService to execute StunStack scheduled tasks,
|
||||||
|
* in particular - expired server transactions collector.
|
||||||
|
*/
|
||||||
|
private static final ScheduledExecutorService tasksScheduler
|
||||||
|
= ExecutorFactory.createSingleThreadScheduledExecutor(
|
||||||
|
"ice4j.StunStack-", 60, TimeUnit.SECONDS);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Our network gateway.
|
* Our network gateway.
|
||||||
*/
|
*/
|
||||||
@ -84,11 +94,12 @@ public class StunStack
|
|||||||
= new Hashtable<>();
|
= new Hashtable<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The <tt>Thread</tt> which expires the <tt>StunServerTransaction</tt>s of
|
* The <tt>ExpiredServerTransactionsCollector</tt> which expires
|
||||||
* this <tt>StunStack</tt> and removes them from
|
* the <tt>StunServerTransaction</tt>s of this <tt>StunStack</tt> and
|
||||||
* {@link #serverTransactions}.
|
* removes them from {@link #serverTransactions}.
|
||||||
*/
|
*/
|
||||||
private Thread serverTransactionExpireThread;
|
private ExpiredServerTransactionsCollector expiredTransactionsCollector
|
||||||
|
= new ExpiredServerTransactionsCollector();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Currently open server transactions. The vector contains transaction ids
|
* Currently open server transactions. The vector contains transaction ids
|
||||||
@ -408,7 +419,6 @@ public class StunStack
|
|||||||
new NetAccessManager(this, peerUdpMessageEventHandler,
|
new NetAccessManager(this, peerUdpMessageEventHandler,
|
||||||
channelDataEventHandler);
|
channelDataEventHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a new <tt>StunStack</tt> instance.
|
* Initializes a new <tt>StunStack</tt> instance.
|
||||||
*/
|
*/
|
||||||
@ -966,7 +976,7 @@ public class StunStack
|
|||||||
synchronized (serverTransactions)
|
synchronized (serverTransactions)
|
||||||
{
|
{
|
||||||
serverTransactions.put(serverTid, sTran);
|
serverTransactions.put(serverTid, sTran);
|
||||||
maybeStartServerTransactionExpireThread();
|
expiredTransactionsCollector.schedule();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1106,6 +1116,8 @@ public class StunStack
|
|||||||
// serverTransactions
|
// serverTransactions
|
||||||
Collection<StunServerTransaction> serverTransactionsToExpire;
|
Collection<StunServerTransaction> serverTransactionsToExpire;
|
||||||
|
|
||||||
|
expiredTransactionsCollector.cancel();
|
||||||
|
|
||||||
synchronized (serverTransactions)
|
synchronized (serverTransactions)
|
||||||
{
|
{
|
||||||
serverTransactionsToExpire
|
serverTransactionsToExpire
|
||||||
@ -1442,135 +1454,6 @@ public class StunStack
|
|||||||
return packetLogger != null && packetLogger.isEnabled();
|
return packetLogger != null && packetLogger.isEnabled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Initializes and starts {@link #serverTransactionExpireThread} if
|
|
||||||
* necessary.
|
|
||||||
*/
|
|
||||||
private void maybeStartServerTransactionExpireThread()
|
|
||||||
{
|
|
||||||
synchronized (serverTransactions)
|
|
||||||
{
|
|
||||||
if (!serverTransactions.isEmpty()
|
|
||||||
&& (serverTransactionExpireThread == null))
|
|
||||||
{
|
|
||||||
Thread t
|
|
||||||
= new Thread()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void run()
|
|
||||||
{
|
|
||||||
runInServerTransactionExpireThread();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
t.setDaemon(true);
|
|
||||||
t.setName(
|
|
||||||
getClass().getName()
|
|
||||||
+ ".serverTransactionExpireThread");
|
|
||||||
|
|
||||||
boolean started = false;
|
|
||||||
|
|
||||||
serverTransactionExpireThread = t;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
t.start();
|
|
||||||
started = true;
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
if (!started && (serverTransactionExpireThread == t))
|
|
||||||
serverTransactionExpireThread = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Runs in {@link #serverTransactionExpireThread} and expires the
|
|
||||||
* <tt>StunServerTransaction</tt>s of this <tt>StunStack</tt> and removes
|
|
||||||
* them from {@link #serverTransactions}.
|
|
||||||
*/
|
|
||||||
private void runInServerTransactionExpireThread()
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
long idleStartTime = -1;
|
|
||||||
|
|
||||||
do
|
|
||||||
{
|
|
||||||
synchronized (serverTransactions)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
serverTransactions.wait(StunServerTransaction.LIFETIME);
|
|
||||||
}
|
|
||||||
catch (InterruptedException ie)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Is the current Thread still designated to expire the
|
|
||||||
* StunServerTransactions of this StunStack?
|
|
||||||
*/
|
|
||||||
if (Thread.currentThread() != serverTransactionExpireThread)
|
|
||||||
break;
|
|
||||||
|
|
||||||
long now = System.currentTimeMillis();
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Has the current Thread been idle long enough to merit
|
|
||||||
* disposing of it?
|
|
||||||
*/
|
|
||||||
if (serverTransactions.isEmpty())
|
|
||||||
{
|
|
||||||
if (idleStartTime == -1)
|
|
||||||
idleStartTime = now;
|
|
||||||
else if (now - idleStartTime > 60 * 1000)
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Expire the StunServerTransactions of this StunStack.
|
|
||||||
|
|
||||||
idleStartTime = -1;
|
|
||||||
|
|
||||||
for (Iterator<StunServerTransaction> i
|
|
||||||
= serverTransactions.values().iterator();
|
|
||||||
i.hasNext();)
|
|
||||||
{
|
|
||||||
StunServerTransaction serverTransaction = i.next();
|
|
||||||
|
|
||||||
if (serverTransaction == null)
|
|
||||||
{
|
|
||||||
i.remove();
|
|
||||||
}
|
|
||||||
else if (serverTransaction.isExpired(now))
|
|
||||||
{
|
|
||||||
i.remove();
|
|
||||||
serverTransaction.expire();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (true);
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
synchronized (serverTransactions)
|
|
||||||
{
|
|
||||||
if (serverTransactionExpireThread == Thread.currentThread())
|
|
||||||
serverTransactionExpireThread = null;
|
|
||||||
/*
|
|
||||||
* If serverTransactionExpireThread dies unexpectedly and yet it
|
|
||||||
* is still necessary, resurrect it.
|
|
||||||
*/
|
|
||||||
if (serverTransactionExpireThread == null)
|
|
||||||
maybeStartServerTransactionExpireThread();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Error Response object with specified errorCode and
|
* Returns the Error Response object with specified errorCode and
|
||||||
* reasonPhrase corresponding to input type.
|
* reasonPhrase corresponding to input type.
|
||||||
@ -1639,4 +1522,117 @@ public class StunStack
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class which performs periodic collection of expired transactions.
|
||||||
|
* It's execution is controlled outside by {@link #schedule()}
|
||||||
|
* and {@link #cancel()} methods. Whenever expired transactions collector
|
||||||
|
* is scheduled it does self reschedule with fixed delay
|
||||||
|
* of StunServerTransaction.LIFETIME_MILLIS,
|
||||||
|
* until {@link #serverTransactions} is empty, in that case it self-cancel
|
||||||
|
* further execution and need to be scheduled again when new item added
|
||||||
|
* to {@link #serverTransactions} container.
|
||||||
|
*/
|
||||||
|
private final class ExpiredServerTransactionsCollector
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Runnable which walks {@link #serverTransactions}, check if
|
||||||
|
* transaction is expired and if so - remove it
|
||||||
|
* from {@link #serverTransactions}.
|
||||||
|
* Self-cancels when {@link #serverTransactions} is empty
|
||||||
|
*/
|
||||||
|
private final Runnable collector = new Runnable()
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void run()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
synchronized (serverTransactions)
|
||||||
|
{
|
||||||
|
final int transactionsBeforeCollection
|
||||||
|
= serverTransactions.size();
|
||||||
|
|
||||||
|
long now = System.currentTimeMillis();
|
||||||
|
|
||||||
|
for (Iterator<StunServerTransaction> i
|
||||||
|
= serverTransactions.values().iterator();
|
||||||
|
i.hasNext();)
|
||||||
|
{
|
||||||
|
StunServerTransaction serverTransaction = i.next();
|
||||||
|
|
||||||
|
if (serverTransaction == null)
|
||||||
|
{
|
||||||
|
i.remove();
|
||||||
|
}
|
||||||
|
else if (serverTransaction.isExpired(now))
|
||||||
|
{
|
||||||
|
i.remove();
|
||||||
|
serverTransaction.expire();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.fine("Non-expired server transactions "
|
||||||
|
+ "count " + serverTransactions.size()
|
||||||
|
+ ", transactions before collection "
|
||||||
|
+ transactionsBeforeCollection);
|
||||||
|
|
||||||
|
if (serverTransactions.isEmpty())
|
||||||
|
{
|
||||||
|
cancel();
|
||||||
|
logger.finest("Cancel expired collector "
|
||||||
|
+ "due to no more server transactions");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Throwable t)
|
||||||
|
{
|
||||||
|
logger.log(Level.FINE,
|
||||||
|
"Failed to expire server transactions", t);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Scheduled execution of {@link #collector} runnable.
|
||||||
|
* Access synchronized via {@link #serverTransactions}.
|
||||||
|
*/
|
||||||
|
private ScheduledFuture<?> scheduledCollectorFuture;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Schedules repeated collector execution in background
|
||||||
|
* task executor. If collector is already scheduled - do nothing
|
||||||
|
*/
|
||||||
|
void schedule()
|
||||||
|
{
|
||||||
|
synchronized (serverTransactions)
|
||||||
|
{
|
||||||
|
if (scheduledCollectorFuture == null ||
|
||||||
|
scheduledCollectorFuture.isDone())
|
||||||
|
{
|
||||||
|
scheduledCollectorFuture
|
||||||
|
= tasksScheduler.scheduleWithFixedDelay(
|
||||||
|
collector,
|
||||||
|
StunServerTransaction.LIFETIME,
|
||||||
|
StunServerTransaction.LIFETIME,
|
||||||
|
TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cancels execution of scheduled expired transactions collector if
|
||||||
|
* it is running
|
||||||
|
*/
|
||||||
|
void cancel()
|
||||||
|
{
|
||||||
|
synchronized (serverTransactions)
|
||||||
|
{
|
||||||
|
if (scheduledCollectorFuture != null)
|
||||||
|
{
|
||||||
|
scheduledCollectorFuture.cancel(false);
|
||||||
|
scheduledCollectorFuture = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
115
src/main/java/org/ice4j/util/ExecutorFactory.java
Normal file
115
src/main/java/org/ice4j/util/ExecutorFactory.java
Normal file
@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* ice4j, the OpenSource Java Solution for NAT and Firewall Traversal.
|
||||||
|
*
|
||||||
|
* Copyright @ 2018 Jitsi.org
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.ice4j.util;
|
||||||
|
|
||||||
|
import java.util.concurrent.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper class which contains functions to create pre-configured executors
|
||||||
|
*/
|
||||||
|
public class ExecutorFactory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Create {@link ScheduledExecutorService} with single executor thread
|
||||||
|
* @param threadNamePrefix - name prefix for threads created by pool
|
||||||
|
* @param threadKeepAliveTime - keep alive time before idle thread is freed
|
||||||
|
* @param timeUnit - time unit of <tt>threadKeepAliveTime</tt>
|
||||||
|
* @return pre-configured {@link ScheduledExecutorService}
|
||||||
|
*/
|
||||||
|
public static ScheduledExecutorService createSingleThreadScheduledExecutor(
|
||||||
|
String threadNamePrefix,
|
||||||
|
int threadKeepAliveTime,
|
||||||
|
TimeUnit timeUnit)
|
||||||
|
{
|
||||||
|
return createScheduledExecutor(
|
||||||
|
1,
|
||||||
|
threadNamePrefix,
|
||||||
|
threadKeepAliveTime,
|
||||||
|
timeUnit);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create {@link ScheduledExecutorService} with number of threads up to
|
||||||
|
* number of CPU cores on machine
|
||||||
|
* @param threadNamePrefix - name prefix for threads created by pool
|
||||||
|
* @param threadKeepAliveTime - keep alive time before idle thread is freed
|
||||||
|
* @param timeUnit - time unit of <tt>threadKeepAliveTime</tt>
|
||||||
|
* @return pre-configured {@link ScheduledExecutorService}
|
||||||
|
*/
|
||||||
|
public static ScheduledExecutorService createCPUBoundScheduledExecutor(
|
||||||
|
String threadNamePrefix,
|
||||||
|
int threadKeepAliveTime,
|
||||||
|
TimeUnit timeUnit)
|
||||||
|
{
|
||||||
|
return createScheduledExecutor(
|
||||||
|
Runtime.getRuntime().availableProcessors(),
|
||||||
|
threadNamePrefix,
|
||||||
|
threadKeepAliveTime,
|
||||||
|
timeUnit);
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Creates pre-configured {@link ScheduledExecutorService} instance with
|
||||||
|
* defaults suitable for ice4j
|
||||||
|
* @param threadNamePrefix - name prefix for threads created by pool
|
||||||
|
* @param poolSize - max number of threads to keep in pool.
|
||||||
|
* @param threadKeepAliveTime - keep alive time before idle thread is freed
|
||||||
|
* @param timeUnit - time unit of <tt>threadKeepAliveTime</tt>
|
||||||
|
* @return pre-configured {@link ScheduledExecutorService}
|
||||||
|
*/
|
||||||
|
private static ScheduledExecutorService createScheduledExecutor(
|
||||||
|
int poolSize,
|
||||||
|
String threadNamePrefix,
|
||||||
|
int threadKeepAliveTime,
|
||||||
|
TimeUnit timeUnit)
|
||||||
|
{
|
||||||
|
CustomizableThreadFactory threadFactory
|
||||||
|
= new CustomizableThreadFactory(threadNamePrefix, true);
|
||||||
|
|
||||||
|
// Motivation:
|
||||||
|
// The desired behaviour from pool is that it creates limited number
|
||||||
|
// of threads based on current load, as well as releasing threads when
|
||||||
|
// there is no work to execute.
|
||||||
|
// Based on these requirements the following default configuration is
|
||||||
|
// chosen.
|
||||||
|
// <tt>corePoolSize</tt> for {@link ScheduledThreadPoolExecutor} is
|
||||||
|
// behaved both as <tt>corePoolSize</tt> and <tt>maxPoolSize</tt>, so
|
||||||
|
// it is actually fixed-size pool.
|
||||||
|
// Even so spec says that corePoolSize is number of
|
||||||
|
// threads to keep in pool they are actually created on demand, so if
|
||||||
|
// there is no load, then threads are not created. But until pool has
|
||||||
|
// less than <tt>corePoolSize</tt> threads, pool will create new thread,
|
||||||
|
// to execute scheduled task, even though already created threads are
|
||||||
|
// idle and have no tasks to execute.
|
||||||
|
// <tt>keepAliveTime</tt> is configurable and specifies timeout before
|
||||||
|
// idle core thread deleted from pool
|
||||||
|
// <tt>removeOnCancelPolicy</tt> is set to true, to immediately remove
|
||||||
|
// queued task from pool queue, because some task might be scheduled
|
||||||
|
// with very big delay causing having reference to creator from pool
|
||||||
|
//
|
||||||
|
// Having <tt>corePoolSize</tt> is set to 0 with unlimited pool size
|
||||||
|
// <tt>maximumPoolSize</tt> is observed to create only 1 thread in pool
|
||||||
|
// no matter how many task are queued and become eligible to execute.
|
||||||
|
final ScheduledThreadPoolExecutor executor
|
||||||
|
= new ScheduledThreadPoolExecutor(poolSize, threadFactory);
|
||||||
|
executor.setKeepAliveTime(threadKeepAliveTime, timeUnit);
|
||||||
|
executor.allowCoreThreadTimeOut(true);
|
||||||
|
executor.setRemoveOnCancelPolicy(true);
|
||||||
|
return Executors.unconfigurableScheduledExecutorService(executor);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user