pub struct ExecutorService<F, T>{ /* private fields */ }
Expand description
The executor service that allows tasks to be submitted/executed on the underlying thread pool.
use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;
let mut executor_service = Executors::new_fixed_thread_pool(2).expect("Failed to create the thread pool");
let some_param = "Mr White";
let res = executor_service.submit_sync(move || {
sleep(Duration::from_secs(5));
println!("Hello {:}", some_param);
println!("Long computation finished");
2
}).expect("Failed to submit function");
println!("Result: {:#?}", res);
assert_eq!(res, 2);
Implementations§
Source§impl<F, T> ExecutorService<F, T>
impl<F, T> ExecutorService<F, T>
Sourcepub fn execute(&mut self, fun: F) -> Result<(), ExecutorServiceError>
pub fn execute(&mut self, fun: F) -> Result<(), ExecutorServiceError>
Execute a function on the thread pool asynchronously with no return.
use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;
use std::thread;
let mut executor_service = Executors::new_fixed_thread_pool(2).expect("Failed to create the thread pool");
let some_param = "Mr White";
let res = executor_service.execute(move || {
sleep(Duration::from_secs(1));
println!("Hello {:} from thread {:}", some_param, thread::current().name().unwrap());
}).expect("Failed to execute function");
sleep(Duration::from_secs(3));
Sourcepub fn submit_sync(&mut self, fun: F) -> Result<T, ExecutorServiceError>
pub fn submit_sync(&mut self, fun: F) -> Result<T, ExecutorServiceError>
Submit a function and wait for its result synchronously
use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;
let mut executor_service = Executors::new_cached_thread_pool(None).expect("Failed to create the thread pool");
let some_param = "Mr White";
let res = executor_service.submit_sync(move || {
sleep(Duration::from_secs(5));
println!("Hello {:}", some_param);
println!("Long computation finished");
2
}).expect("Failed to submit function");
println!("Result: {:#?}", res);
assert_eq!(res, 2);
Sourcepub fn submit_async(
&mut self,
fun: F,
) -> Result<Future<T>, ExecutorServiceError>
pub fn submit_async( &mut self, fun: F, ) -> Result<Future<T>, ExecutorServiceError>
Submit a function and get a Future object to obtain the result asynchronously when needed.
use executor_service::Executors;
use std::thread::sleep;
use core::time::Duration;
let mut executor_service = Executors::new_cached_thread_pool(Some(5)).expect("Failed to create the thread pool");
let some_param = "Mr White";
let future = executor_service.submit_async(Box::new(move || {
sleep(Duration::from_secs(3));
println!("Hello {:}", some_param);
println!("Long computation finished");
"Some string result".to_string()
})).expect("Failed to submit function");
//Wait a bit more to see the future work.
println!("Main thread wait for 5 seconds");
sleep(Duration::from_secs(5));
let res = future.get().expect("Couldn't get a result");
println!("Result is {:}", &res);
assert_eq!(&res, "Some string result");
pub fn pool_type(&self) -> &PoolType
pub fn get_thread_count(&self) -> Result<u32, ExecutorServiceError>
Trait Implementations§
Auto Trait Implementations§
impl<F, T> Freeze for ExecutorService<F, T>
impl<F, T> RefUnwindSafe for ExecutorService<F, T>
impl<F, T> Send for ExecutorService<F, T>
impl<F, T> Sync for ExecutorService<F, T>
impl<F, T> Unpin for ExecutorService<F, T>
impl<F, T> UnwindSafe for ExecutorService<F, T>
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