Class ZeroSizeThreadPool
This is class is a simple implementation of a zero size thread pool, based on the IThreadPool interface.
public sealed class ZeroSizeThreadPool : IThreadPool
- Inheritance
-
ZeroSizeThreadPool
- Implements
- Inherited Members
Remarks
The pool has zero Threads and does not grow or shrink based on demand. Which means it is obviously not useful for most scenarios. When it may be useful is to prevent creating any worker threads at all - which may be desirable for the sole purpose of preserving system resources in the case where the scheduler instance only exists in order to schedule jobs, but which will never execute jobs (e.g. will never have Start() called on it).
Constructors
ZeroSizeThreadPool(ILogger<ZeroSizeThreadPool>?)
Initializes a new instance of the ZeroSizeThreadPool class.
public ZeroSizeThreadPool(ILogger<ZeroSizeThreadPool>? logger = null)
Parameters
loggerILogger<ZeroSizeThreadPool>Where the pool reports the calls it refuses. Filled in by the container when it builds the pool; a pool constructed by hand is handed nothing and reads LogProvider, as before.
Properties
PoolSize
Gets the size of the pool.
public int PoolSize { get; }
Property Value
- int
The size of the pool.
Methods
Initialize(CancellationToken)
Called by the QuartzScheduler before the thread pool is used, in order to give the it a chance to Initialize.
public ValueTask Initialize(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe cancellation instruction.
Returns
Shutdown(bool, CancellationToken)
Called by the QuartzScheduler to inform the thread pool that it should free up all of it's resources because the scheduler is shutting down.
public ValueTask Shutdown(bool waitForJobsToComplete = true, CancellationToken cancellationToken = default)
Parameters
waitForJobsToCompleteboolIgnored: this pool runs nothing, so there is never anything to wait for.
cancellationTokenCancellationTokenThe cancellation instruction.
Returns
TryRun(Func<ValueTask>, CancellationToken)
public ValueTask<bool> TryRun(Func<ValueTask> action, CancellationToken cancellationToken = default)
Parameters
actionFunc<ValueTask>The work to run.
cancellationTokenCancellationTokenThe cancellation instruction.
Returns
Remarks
The implementation of this interface should not throw exceptions unless there is a serious problem (i.e. a serious misconfiguration). If there are no available threads, rather it should either queue the action, or wait until a thread is available, depending on the desired strategy.
WaitForAvailableThreads(CancellationToken)
Determines the number of threads that are currently available in the pool. Useful for determining the number of times TryRun(Func<ValueTask>, CancellationToken) can be called before returning false.
public ValueTask<int> WaitForAvailableThreads(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenThe cancellation instruction.
Returns
Remarks
The implementation of this method should wait until there is at least one available thread.