Skip to main content

ThreadPoolBuilder

Struct ThreadPoolBuilder 

Source
pub struct ThreadPoolBuilder { /* private fields */ }
Expand description

Builder for ThreadPool.

The default builder uses the available CPU parallelism as both core and maximum pool size, with an unbounded FIFO queue.

Implementations§

Source§

impl ThreadPoolBuilder

Source

pub fn pool_size(self, pool_size: usize) -> Self

Sets both the core and maximum pool size to the same value.

§Parameters
  • pool_size - Pool size applied as both core and maximum limits.
§Returns

This builder for fluent configuration.

Source

pub fn core_pool_size(self, core_pool_size: usize) -> Self

Sets the core pool size.

A submitted task creates a new worker while the live worker count is below this value. Once the core size is reached, tasks are queued before the pool considers growing toward the maximum size.

§Parameters
  • core_pool_size - Core pool size.
§Returns

This builder for fluent configuration.

Source

pub fn maximum_pool_size(self, maximum_pool_size: usize) -> Self

Sets the maximum pool size.

The pool grows above the core size only when the queue cannot accept a submitted task.

§Parameters
  • maximum_pool_size - Maximum pool size.
§Returns

This builder for fluent configuration.

Source

pub fn queue_capacity(self, capacity: usize) -> Self

Sets a bounded queue capacity.

The capacity counts only tasks waiting in the queue. Tasks already held by worker threads are not included.

§Parameters
  • capacity - Maximum number of queued tasks.
§Returns

This builder for fluent configuration.

Source

pub fn unbounded_queue(self) -> Self

Uses an unbounded queue.

§Returns

This builder for fluent configuration.

Source

pub fn thread_name_prefix(self, prefix: &str) -> Self

Sets the worker thread name prefix.

Worker names are created by appending the worker index to this prefix.

§Parameters
  • prefix - Prefix for worker thread names.
§Returns

This builder for fluent configuration.

Source

pub fn stack_size(self, stack_size: usize) -> Self

Sets the worker thread stack size.

§Parameters
  • stack_size - Stack size in bytes for each worker thread.
§Returns

This builder for fluent configuration.

Source

pub fn keep_alive(self, keep_alive: Duration) -> Self

Sets the idle timeout for workers above the core pool size.

§Parameters
  • keep_alive - Duration an excess worker may stay idle.
§Returns

This builder for fluent configuration.

Source

pub fn allow_core_thread_timeout(self, allow: bool) -> Self

Allows core workers to retire after the keep-alive timeout.

§Parameters
  • allow - Whether idle core workers may time out.
§Returns

This builder for fluent configuration.

Source

pub fn prestart_core_threads(self) -> Self

Starts all core workers during Self::build.

Without this option, workers are created lazily as tasks are submitted, matching the default JDK ThreadPoolExecutor behavior.

§Returns

This builder for fluent configuration.

Source

pub fn build(self) -> Result<ThreadPool, ThreadPoolBuildError>

Builds the configured thread pool.

§Returns

Ok(ThreadPool) if the configuration is valid and all requested prestarted workers are spawned successfully.

§Errors

Returns ThreadPoolBuildError if the configuration is invalid or a prestarted worker thread cannot be spawned.

Trait Implementations§

Source§

impl Clone for ThreadPoolBuilder

Source§

fn clone(&self) -> ThreadPoolBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ThreadPoolBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ThreadPoolBuilder

Source§

fn default() -> Self

Creates a builder with CPU parallelism defaults.

§Returns

A builder configured with CPU parallelism for both core and maximum sizes, an unbounded queue, and the default keep-alive timeout.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.