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
impl ThreadPoolBuilder
Sourcepub fn channel_capacity(self, capacity: usize) -> Self
pub fn channel_capacity(self, capacity: usize) -> Self
Sets the capacity of the bounded task channel.
Sourcepub fn max_pool_size(self, size: usize) -> Self
pub fn max_pool_size(self, size: usize) -> Self
Sets the maximum allowed number of threads.
Sourcepub fn core_pool_size(self, size: usize) -> Self
pub fn core_pool_size(self, size: usize) -> Self
Sets the number of core threads.
Sourcepub fn keep_alive_time(self, time: Duration) -> Self
pub fn keep_alive_time(self, time: Duration) -> Self
Sets the time that non-core threads may remain idle.
Sourcepub fn rejected_handler(self, handler: RejectedTaskHandler) -> Self
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.
Sourcepub fn lisenter_before_execute<F>(self, executor: F) -> Self
pub fn lisenter_before_execute<F>(self, executor: F) -> Self
Sets the listener function that will be invoked before a task is executed.
Sourcepub fn lisenter_after_execute<F>(self, executor: F) -> Self
pub fn lisenter_after_execute<F>(self, executor: F) -> Self
Sets the listener function that will be invoked after a task is executed.
Sourcepub fn thread_factory_fn<F>(self, f: F) -> Self
pub fn thread_factory_fn<F>(self, f: F) -> Self
Sets the factory function that is used to create a new custom thread.
Sourcepub fn build(self) -> ThreadPool
pub fn build(self) -> ThreadPool
Trait Implementations§
Source§impl Default for ThreadPoolBuilder
impl Default for ThreadPoolBuilder
Source§fn default() -> Self
fn default() -> Self
Creates a new builder with the default configuration.
§Default Configuration
channel_capacity: 1000max_pool_size: the number of physical cores of the current systemcore_pool_size: the half of themax_pool_sizeat least 1keep_alive_time: 1 secondrejected_task_handler:RejectedTaskHandler::Abortbefore_execute: an empty closure|_| ()after_execute: an empty closure|_| ()thread_factory:|| thread::Builder::new()
Auto Trait Implementations§
impl Freeze for ThreadPoolBuilder
impl !RefUnwindSafe for ThreadPoolBuilder
impl Send for ThreadPoolBuilder
impl Sync for ThreadPoolBuilder
impl Unpin for ThreadPoolBuilder
impl !UnwindSafe for ThreadPoolBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more