ThreadPoolBuilder

Struct ThreadPoolBuilder 

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

A builder of the ThreadPool, which can be used to configure the properties of a new thread pool.

§Examples

use jtp::ThreadPoolBuilder;
use jtp::RejectedTaskHandler;
use std::time::Duration;

let thread_pool = ThreadPoolBuilder::default()
    .core_pool_size(4)
    .max_pool_size(7)
    .keep_alive_time(Duration::from_secs(2))
    .rejected_handler(RejectedTaskHandler::Discard)
    .lisenter_before_execute(|id| println!("the task {} will be executed.", id))
    .lisenter_before_execute(|id| println!("the task {} has been executed.", id))
    .thread_factory_fn(|| {
        std::thread::Builder::new().stack_size(1024 * 4)
    })
    .build();

Implementations§

Source§

impl ThreadPoolBuilder

Source

pub fn new() -> Self

Creates the base configuration for the new thread pool.

See: ThreadPoolBuilder::default

Source

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

Sets the capacity of the bounded task channel.

Source

pub fn max_pool_size(self, size: usize) -> Self

Sets the maximum allowed number of threads.

Source

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

Sets the number of core threads.

Source

pub fn keep_alive_time(self, time: Duration) -> Self

Sets the time that non-core threads may remain idle.

Source

pub fn rejected_handler(self, handler: RejectedTaskHandler) -> Self

Sets the policy to handle rejected tasks.

When you execute a task, it will be handled by the given handler if the thread pool and the task channel are both full.

Source

pub fn lisenter_before_execute<F>(self, executor: F) -> Self
where F: Fn(usize) + Send + Sync + 'static,

Sets the listener function that will be invoked before a task is executed.

Source

pub fn lisenter_after_execute<F>(self, executor: F) -> Self
where F: Fn(usize) + Send + Sync + 'static,

Sets the listener function that will be invoked after a task is executed.

Source

pub fn thread_factory_fn<F>(self, f: F) -> Self
where F: Fn() -> Builder + Send + Sync + 'static,

Sets the factory function that is used to create a new custom thread.

Source

pub fn build(self) -> ThreadPool

Creates a thread pool with the arguments.

§Panics

Panics if the builder with invalid arguments.

Trait Implementations§

Source§

impl Default for ThreadPoolBuilder

Source§

fn default() -> Self

Creates a new builder with the default configuration.

§Default Configuration
  • channel_capacity: 1000
  • max_pool_size: the number of physical cores of the current system
  • core_pool_size: the half of the max_pool_size at least 1
  • keep_alive_time: 1 second
  • rejected_task_handler: RejectedTaskHandler::Abort
  • before_execute: an empty closure |_| ()
  • after_execute: an empty closure |_| ()
  • thread_factory: || thread::Builder::new()

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> 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, 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.