- Type Parameters:
T- The concrete poolable type.
- All Implemented Interfaces:
Cloneable,PoolBuilder<T>
PoolBuilder implementation.-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Map<AllocationProcessMode, PoolBuilderDefaults> Mapping ofAllocationProcessModeto the pool builder default settings.static final Map<AllocationProcessMode, PoolBuilderPermissions> Mapping ofAllocationProcessModeto the pool builder permissions, deciding what settings can be changed. -
Constructor Summary
ConstructorsConstructorDescriptionPoolBuilderImpl(AllocationProcess allocationProcess, Allocator<T> allocator) Build a new emptyPoolBuilderobject. -
Method Summary
Modifier and TypeMethodDescriptionbuild()Build aPoolinstance based on the collected configuration.clone()Returns a shallow copy of thisPoolBuilderobject.Get aReallocatorfrom this pool builder, possibly by adapting the configuredAllocator.Get the configuredAllocatorinstance.intReturn the default approximate delay, in milliseconds, between background maintenance tasks, such as the background expiration checks and retrying failed allocations.Expiration<? super T> Get the configuredExpirationinstance.Get the configuredMetricsRecorderinstance, ornullif none has been configured.Get the configuredAllocatorinstance as aReallocator.longgetSize()Get the currently configured size.Get the ThreadFactory that has been configured, and will be used to create the background allocation threads for the pools.booleanReturn whether background expiration is enabled.booleanReturn whether Stormpot will prioritize minimizing its memory overhead over maximizing performance.booleanReturn whether precise object leak detection is enabled, which is the case by default.<X extends Poolable>
PoolBuilder<X> setAllocator(Allocator<X> allocator) Set theAllocatororReallocatorto use for the pools we want to configure.setBackgroundExpirationCheckDelay(int delay) Set the approximate delay, in milliseconds, between background maintenance tasks.setBackgroundExpirationEnabled(boolean enabled) Enable or disable background object expiration checking.setExpiration(Expiration<? super T> expiration) Set theExpirationto use for the pools we want to configure.setMetricsRecorder(MetricsRecorder metricsRecorder) Set theMetricsRecorderto use for the pools we want to configure.setOptimizeForReducedMemoryUsage(boolean reduceMemoryUsage) Tell the pool to optimize for either low memory usage (when givingtrue), or maximal performance (when givingfalse).setPreciseLeakDetectionEnabled(boolean enabled) Enable or disable precise object leak detection.setSize(long size) Set the size of the pool we are building.setThreadFactory(ThreadFactory factory) Set the ThreadFactory that the pools will use to create its background threads with.
-
Field Details
-
DEFAULTS
Mapping ofAllocationProcessModeto the pool builder default settings. -
PERMISSIONS
Mapping ofAllocationProcessModeto the pool builder permissions, deciding what settings can be changed.
-
-
Constructor Details
-
PoolBuilderImpl
Build a new emptyPoolBuilderobject.- Parameters:
allocationProcess- The allocation process to use.allocator- The allocator instance to use.
-
-
Method Details
-
setSize
Description copied from interface:PoolBuilderSet the size of the pool we are building.Pools are required to control the allocations and deallocations, such that no more than this number of objects are allocated at any time.
This means that a pool of size 1, whose single object have expired, will deallocate that one object before allocating a replacement.
The size must be at least zero, or an
IllegalArgumentExceptionwill be thrown when building the pool.Note that the pool size can be modified after the pool has been built, by calling the
Pool.setTargetSize(long)orManagedPool.setTargetSize(long)methods.- Specified by:
setSizein interfacePoolBuilder<T extends Poolable>- Parameters:
size- The target pool size. Must be at least 0.- Returns:
- This
PoolBuilderinstance.
-
getSize
public long getSize()Description copied from interface:PoolBuilderGet the currently configured size. The default is 10.- Specified by:
getSizein interfacePoolBuilder<T extends Poolable>- Returns:
- The configured pool size.
-
setAllocator
Description copied from interface:PoolBuilderSet theAllocatororReallocatorto use for the pools we want to configure. This will change the type-parameter of thePoolBuilderobject to match that of the new Allocator.The allocator is initially specified by the
Pool.from(Allocator)method, so there is usually no need to set it later.- Specified by:
setAllocatorin interfacePoolBuilder<T extends Poolable>- Type Parameters:
X- The type ofPoolablethat is created by the allocator, and the type of objects that the configured pools will contain.- Parameters:
allocator- The allocator we want our pools to use. This cannot benull.- Returns:
- This
PoolBuilderinstance, but with a generic type parameter that matches that of the allocator.
-
getAllocator
Description copied from interface:PoolBuilderGet the configuredAllocatorinstance.- Specified by:
getAllocatorin interfacePoolBuilder<T extends Poolable>- Returns:
- The configured Allocator instance.
-
getReallocator
Description copied from interface:PoolBuilderGet the configuredAllocatorinstance as aReallocator. If the configured allocator implements the Reallocator interface, then it is returned directly. Otherwise, the allocator is wrapped in an adaptor.- Specified by:
getReallocatorin interfacePoolBuilder<T extends Poolable>- Returns:
- A configured or adapted Reallocator.
-
setExpiration
Description copied from interface:PoolBuilderSet theExpirationto use for the pools we want to configure. The Expiration determines when a pooled object is valid for claiming, or when the objects are invalid and should be deallocated.The default Expiration is an
Expiration.after(long, long, TimeUnit)that invalidates the objects after they have been active for somewhere from 8 to 10 minutes.- Specified by:
setExpirationin interfacePoolBuilder<T extends Poolable>- Parameters:
expiration- The expiration we want our pools to use. Not null.- Returns:
- This
PoolBuilderinstance.
-
getExpiration
Description copied from interface:PoolBuilderGet the configuredExpirationinstance. The default is aExpiration.after(long, long, TimeUnit)that expires objects after somewhere from 8 to 10 minutes.- Specified by:
getExpirationin interfacePoolBuilder<T extends Poolable>- Returns:
- The configured Expiration.
-
setMetricsRecorder
Description copied from interface:PoolBuilderSet theMetricsRecorderto use for the pools we want to configure.- Specified by:
setMetricsRecorderin interfacePoolBuilder<T extends Poolable>- Parameters:
metricsRecorder- The MetricsRecorder to use, or null if we don't want to use any.- Returns:
- This
PoolBuilderinstance.
-
getMetricsRecorder
Description copied from interface:PoolBuilderGet the configuredMetricsRecorderinstance, ornullif none has been configured.- Specified by:
getMetricsRecorderin interfacePoolBuilder<T extends Poolable>- Returns:
- The configured MetricsRecorder.
-
getThreadFactory
Description copied from interface:PoolBuilderGet the ThreadFactory that has been configured, and will be used to create the background allocation threads for the pools. The default is similar to theExecutors.defaultThreadFactory(), except the string "Stormpot-" is prepended to the thread name.- Specified by:
getThreadFactoryin interfacePoolBuilder<T extends Poolable>- Returns:
- The configured thread factory.
-
setThreadFactory
Description copied from interface:PoolBuilderSet the ThreadFactory that the pools will use to create its background threads with. The ThreadFactory is not allowed to be null, and creating a pool with a null ThreadFactory will throw an IllegalArgumentException.- Specified by:
setThreadFactoryin interfacePoolBuilder<T extends Poolable>- Parameters:
factory- The ThreadFactory the pool should use to create their background threads.- Returns:
- This
PoolBuilderinstance.
-
isPreciseLeakDetectionEnabled
public boolean isPreciseLeakDetectionEnabled()Description copied from interface:PoolBuilderReturn whether precise object leak detection is enabled, which is the case by default.- Specified by:
isPreciseLeakDetectionEnabledin interfacePoolBuilder<T extends Poolable>- Returns:
trueif precise object leak detection is enabled.- See Also:
-
setPreciseLeakDetectionEnabled
Description copied from interface:PoolBuilderEnable or disable precise object leak detection. It is enabled by default. Precise object leak detection makes the pool keep an eye on the allocated Poolables, such that it notices if they get garbage collected without first being deallocated. Using the garbage collector for this purpose, means that no false positives (counting objects as leaked, even though they are not) are ever reported.
Precise object leak detection incurs virtually no overhead, and is safe to leave enabled at all times – even in the most demanding production environments.Note NOTE While the pool is able to detect object leaks, it cannot prevent them. All leaks are a sign that there is a bug in the system; most likely a bug in your code, or in the way the pool is used. - Specified by:
setPreciseLeakDetectionEnabledin interfacePoolBuilder<T extends Poolable>- Parameters:
enabled-trueto turn on precise object leak detection (the default)falseto turn it off.- Returns:
- This
PoolBuilderinstance.
-
isBackgroundExpirationEnabled
public boolean isBackgroundExpirationEnabled()Description copied from interface:PoolBuilderReturn whether background expiration is enabled. By default, background expiration is enabled.- Specified by:
isBackgroundExpirationEnabledin interfacePoolBuilder<T extends Poolable>- Returns:
trueif background expiration is enabled.- See Also:
-
setBackgroundExpirationEnabled
Description copied from interface:PoolBuilderEnable or disable background object expiration checking. This is enabled by default, but can be turned off if the check is expensive. The cost of the check matters because it might end up taking resources away from the background thread and hinder its ability to keep up with the demand for allocations and deallocations, even though these tasks always take priority over any expiration checking.- Specified by:
setBackgroundExpirationEnabledin interfacePoolBuilder<T extends Poolable>- Parameters:
enabled-true(the default) to turn background expiration checking on,falseto turn it off.- Returns:
- This
PoolBuilderinstance.
-
getBackgroundExpirationCheckDelay
public int getBackgroundExpirationCheckDelay()Description copied from interface:PoolBuilderReturn the default approximate delay, in milliseconds, between background maintenance tasks, such as the background expiration checks and retrying failed allocations.- Specified by:
getBackgroundExpirationCheckDelayin interfacePoolBuilder<T extends Poolable>- Returns:
- the delay, in milliseconds, between background maintenance tasks.
-
setBackgroundExpirationCheckDelay
Description copied from interface:PoolBuilderSet the approximate delay, in milliseconds, between background maintenance tasks. These tasks include the background expiration checks, and retrying failed allocations.The default delay is 1.000 milliseconds (1 second). Lowering this value will improve the pools responsiveness to repairing failed allocations, and also increase the frequency of the background expiration checks. This comes at the cost of higher idle CPU usage.
It is not recommended to set this value lower than 100 milliseconds. Values lower than this tend to have increased CPU and power usage, for very little gain in responsiveness for the background tasks.
- Specified by:
setBackgroundExpirationCheckDelayin interfacePoolBuilder<T extends Poolable>- Parameters:
delay- the desired delay, in milliseconds, between background maintenance tasks.- Returns:
- This
PoolBuilderinstance.
-
isOptimizeForReducedMemoryUsage
public boolean isOptimizeForReducedMemoryUsage()Description copied from interface:PoolBuilderReturn whether Stormpot will prioritize minimizing its memory overhead over maximizing performance.This is
trueby default, as the performance gains don't show except in intense and highly concurrent use cases.- Specified by:
isOptimizeForReducedMemoryUsagein interfacePoolBuilder<T extends Poolable>- Returns:
truefor prioritizing memory usage over absolute performance, otherwisefalsefor prioritizing performance at all costs.
-
setOptimizeForReducedMemoryUsage
Description copied from interface:PoolBuilderTell the pool to optimize for either low memory usage (when givingtrue), or maximal performance (when givingfalse).This is
trueby default, and should only be set tofalsewhen the pool is expected to be "relatively small" and will experience an extremely high level of multithreaded access.- Specified by:
setOptimizeForReducedMemoryUsagein interfacePoolBuilder<T extends Poolable>- Parameters:
reduceMemoryUsage- whether to prioritize memory usage or performance.- Returns:
- This
PoolBuilderinstance.
-
clone
Description copied from interface:PoolBuilderReturns a shallow copy of thisPoolBuilderobject. -
build
Description copied from interface:PoolBuilderBuild aPoolinstance based on the collected configuration.- Specified by:
buildin interfacePoolBuilder<T extends Poolable>- Returns:
- A
Poolinstance as configured by this builder.
-
getAdaptedReallocator
Get aReallocatorfrom this pool builder, possibly by adapting the configuredAllocator.- Returns:
- A
Reallocatorinstance, either the one given to the pool builder, or a new adapted instance.
-