Struct executor_service::ExecutorService
source · pub struct ExecutorService<F, T>where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,{ /* 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>where
F: FnOnce() -> T + Send + 'static,
T: Send + 'static,
impl<F, T> ExecutorService<F, T>where F: FnOnce() -> T + Send + 'static, T: Send + 'static,
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");