Struct executor_service::ExecutorService
source · pub struct ExecutorService { /* 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);
let some_param = "Mr White";
let res = executor_service.submit_sync(Box::new(move || {
sleep(Duration::from_secs(5));
println!("Hello {:}", some_param);
println!("Long lasting computation finished");
2
})).expect("Failed to submit function");
println!("Result: {:#?}", res);
assert_eq!(res, 2);Implementations§
source§impl ExecutorService
impl ExecutorService
sourcepub fn execute(&mut self, fun: Runnable) -> Result<(), ExecutorServiceError>
pub fn execute(&mut self, fun: Runnable) -> 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);
let some_param = "Mr White";
let res = executor_service.execute(Box::new(move || {
sleep(Duration::from_secs(1));
println!("Hello from thread {:}", thread::current().name().unwrap());
})).expect("Failed to execute function");
sleep(Duration::from_secs(3));sourcepub fn submit_sync<T: Sync + Send + 'static>(
&mut self,
fun: Callable<T>
) -> Result<T, ExecutorServiceError>
pub fn submit_sync<T: Sync + Send + 'static>( &mut self, fun: Callable<T> ) -> 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_fixed_thread_pool(2);
let some_param = "Mr White";
let res = executor_service.submit_sync(Box::new(move || {
sleep(Duration::from_secs(5));
println!("Hello {:}", some_param);
println!("Long lasting computation finished");
2
})).expect("Failed to submit function");
println!("Result: {:#?}", res);
assert_eq!(res, 2);sourcepub fn submit_async<T: Sync + Send + 'static>(
&mut self,
fun: Callable<T>
) -> Result<Future<T>, ExecutorServiceError>
pub fn submit_async<T: Sync + Send + 'static>( &mut self, fun: Callable<T> ) -> 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_fixed_thread_pool(2);
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 lasting 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");