Skip to main content

ExecutorService

Trait ExecutorService 

Source
pub trait ExecutorService: Send + Sync {
    type Handle<R: Send + 'static, E: Send + 'static>;
    type Termination<'a>: Future<Output = ()> + Send + 'a
       where Self: 'a;

    // Required methods
    fn submit_callable<C, R, E>(
        &self,
        task: C,
    ) -> Result<Self::Handle<R, E>, RejectedExecution>
       where C: Callable<R, E> + Send + 'static,
             R: Send + 'static,
             E: Send + 'static;
    fn shutdown(&self);
    fn shutdown_now(&self) -> ShutdownReport;
    fn is_shutdown(&self) -> bool;
    fn is_terminated(&self) -> bool;
    fn await_termination(&self) -> Self::Termination<'_>;

    // Provided method
    fn submit<T, E>(
        &self,
        task: T,
    ) -> Result<Self::Handle<(), E>, RejectedExecution>
       where T: Runnable<E> + Send + 'static,
             E: Send + 'static { ... }
}
Expand description

Managed task service with submission and lifecycle control.

ExecutorService is intentionally separate from Executor. An executor describes an execution strategy; an executor service accepts tasks into a managed service that may queue, schedule, assign workers, and track lifecycle.

submit and submit_callable return Result values whose outer Ok means only that the service accepted the task. It does not mean the task has started or succeeded. The task’s final result is observed through the returned handle.

Required Associated Types§

Source

type Handle<R: Send + 'static, E: Send + 'static>

Handle returned for an accepted task.

Source

type Termination<'a>: Future<Output = ()> + Send + 'a where Self: 'a

Future returned when waiting for service termination.

Required Methods§

Source

fn submit_callable<C, R, E>( &self, task: C, ) -> Result<Self::Handle<R, E>, RejectedExecution>
where C: Callable<R, E> + Send + 'static, R: Send + 'static, E: Send + 'static,

Submits a callable task to this service.

§Parameters
  • task - A fallible computation whose success value should be captured in the returned handle.
§Returns

Ok(handle) if the service accepts the task. This only reports acceptance; task success, task failure, panic, or cancellation must be observed through the returned handle. Returns Err(RejectedExecution) if the service refuses the task before accepting it.

§Errors

Returns RejectedExecution when the service refuses the task before accepting it.

Source

fn shutdown(&self)

Initiates an orderly shutdown.

After shutdown starts, new tasks are rejected. Already accepted tasks are allowed to complete unless the concrete service documents stronger cancellation behavior.

Source

fn shutdown_now(&self) -> ShutdownReport

Attempts to stop accepting and running tasks immediately.

§Returns

A count-based shutdown report describing the state observed at the time of the request.

Source

fn is_shutdown(&self) -> bool

Returns whether shutdown has been requested.

§Returns

true if this service is no longer accepting new tasks.

Source

fn is_terminated(&self) -> bool

Returns whether the service has terminated.

§Returns

true only after shutdown has been requested and all accepted tasks have completed or been cancelled.

Source

fn await_termination(&self) -> Self::Termination<'_>

Waits until the service has terminated.

§Returns

A future that completes after shutdown has been requested and no accepted tasks remain active.

Provided Methods§

Source

fn submit<T, E>( &self, task: T, ) -> Result<Self::Handle<(), E>, RejectedExecution>
where T: Runnable<E> + Send + 'static, E: Send + 'static,

Submits a runnable task to this service.

§Parameters
  • task - A fallible background action with no business return value.
§Returns

Ok(handle) if the service accepts the task. This only reports acceptance; it does not report task start or task success. Returns Err(RejectedExecution) if the service refuses the task before accepting it.

§Errors

Returns RejectedExecution when the service refuses the task before accepting it.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl ExecutorService for FixedThreadPool

Source§

type Handle<R, E> = TaskHandle<R, E> where R: Send + 'static, E: Send + 'static

Source§

type Termination<'a> = Pin<Box<dyn Future<Output = ()> + Send + 'a>> where Self: 'a

Source§

impl ExecutorService for ThreadPool

Source§

type Handle<R, E> = TaskHandle<R, E> where R: Send + 'static, E: Send + 'static

Source§

type Termination<'a> = Pin<Box<dyn Future<Output = ()> + Send + 'a>> where Self: 'a

Source§

impl ExecutorService for ThreadPerTaskExecutorService

Source§

type Handle<R: Send + 'static, E: Send + 'static> = TaskHandle<R, E>

Source§

type Termination<'a> = Pin<Box<dyn Future<Output = ()> + Send + 'a>> where ThreadPerTaskExecutorService: 'a