ExecutorService 0.4.0
See the version list below for details.
dotnet add package ExecutorService --version 0.4.0
NuGet\Install-Package ExecutorService -Version 0.4.0
<PackageReference Include="ExecutorService" Version="0.4.0" />
<PackageVersion Include="ExecutorService" Version="0.4.0" />
<PackageReference Include="ExecutorService" />
paket add ExecutorService --version 0.4.0
#r "nuget: ExecutorService, 0.4.0"
#:package ExecutorService@0.4.0
#addin nuget:?package=ExecutorService&version=0.4.0
#tool nuget:?package=ExecutorService&version=0.4.0
ExecutorService
A .NET port of Java's
java.util.concurrent.ExecutorService:
bounded executors backed by dedicated threads, an explicit lifecycle (Shutdown, ShutdownNow,
AwaitTermination), and Task-based futures.
Why?
The .NET ThreadPool is a single, process-wide, elastic pool. That is the right default for async I/O, but sometimes
you want what Java developers reach for with Executors.newFixedThreadPool(n):
- A fixed number of threads for a specific workload, isolated from the rest of the process.
- Safe blocking work without starving the shared pool.
- A deterministic shutdown: stop accepting work, drain or drop the queue, wait for termination.
- Strict FIFO and, with one thread, strictly sequential execution.
Installation
dotnet add package ExecutorService
Requires .NET 10 or later.
Quick start
using ExecutorService;
using IExecutorService executor = Executors.NewFixedThreadPool(4);
// Fire and forget
executor.Execute(() => Console.WriteLine("hello from a pool thread"));
// Submit and await a result
Task<int> future = executor.Submit(() => ComputeSomethingExpensive());
int result = await future;
// Orderly shutdown: finish queued work, reject new work
executor.Shutdown();
bool terminated = executor.AwaitTermination(TimeSpan.FromSeconds(10));
Disposing the executor is equivalent to Java's close(): it calls Shutdown() and waits for termination.
await using does the same without blocking.
Dropping pending work
IReadOnlyList<Task> neverStarted = executor.ShutdownNow();
// each Task in `neverStarted` is in the Canceled state
Tasks that are already running are not interrupted. .NET has no thread interruption; implement cooperative cancellation inside your task if you need it.
Configuring worker threads
var executor = Executors.NewFixedThreadPool(2, new ThreadPoolExecutorOptions
{
ThreadNamePrefix = "image-resizer", // threads are named image-resizer-0, image-resizer-1
IsBackground = false, // keep the process alive while work is pending
Priority = ThreadPriority.BelowNormal,
});
Java to .NET mapping
| Java | ExecutorService (.NET) |
|---|---|
Executor |
IExecutor |
ExecutorService |
IExecutorService |
Executors.newFixedThreadPool(n) |
Executors.NewFixedThreadPool(n) |
Executors.newSingleThreadExecutor() |
Executors.NewSingleThreadExecutor() |
execute(Runnable) |
Execute(Action) |
submit(Runnable) / submit(Callable) |
Submit(Action) / Submit<T>(Func<T>) |
Future<T> |
Task<T> |
shutdown() / shutdownNow() |
Shutdown() / ShutdownNow() |
awaitTermination(timeout, unit) |
AwaitTermination(TimeSpan) / AwaitTerminationAsync(TimeSpan, CancellationToken) |
isShutdown() / isTerminated() |
IsShutdown / IsTerminated |
close() |
Dispose() / DisposeAsync() |
RejectedExecutionException |
RejectedExecutionException |
Roadmap
See CHANGELOG.md for released features. Planned:
InvokeAll/InvokeAny- Cancellation-aware overloads (
Func<CancellationToken, T>) andShutdownNowpropagating a token - Async task overloads (
Func<Task>,Func<Task<T>>) - Bounded queues with rejection policies (
Abort,CallerRuns,Discard,DiscardOldest) Executors.NewCachedThreadPool()with core / max pool size and keep-aliveIScheduledExecutorService(Schedule,ScheduleAtFixedRate,ScheduleWithFixedDelay)TaskScheduleradapter soTask.Factory.StartNewcan target an executor
Contributing
Contributions are welcome. Please read CONTRIBUTING.md and the Code of Conduct.
License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.