Repository: tokio-rs/tokio
File: tokio/src/runtime/task/join.rs (Rust)

Code before:
    ///    for handle in &handles {
    ///        handle.abort();
    ///    }
    ///
    ///    for handle in handles {
    ///        assert!(handle.await.unwrap_err().is_cancelled());
    ///    }
    /// }
    /// ```
    /// [cancelled]: method@super::error::JoinError::is_cancelled
    pub fn abort(&self) {
        if let Some(raw) = self.raw {
            raw.remote_abort();
        }
    }

    /// Checks if the task associated with the handle has finished(whether completed or cancelled).
    /// This function does not block.
    ///
    /// ```rust
    /// use tokio::time;
    ///
    /// #[tokio::main]
    /// async fn main() {
    ///    let handle1 = tokio::spawn(async {
    ///        // do some stuff here
    ///    });
    ///    let handle2 = tokio::spawn(async {
    ///        // do some other stuff here
    ///        time::sleep(time::Duration::from_secs(10)).await;
    ///    });
    ///    // Wait for the task to finish
    ///    handle2.abort();
    ///    time::sleep(time::Duration::from_secs(1)).await;
    ///    assert!(handle1.is_finished());
    ///    assert!(handle2.is_finished());
    /// }
    /// ```
    pub fn is_finished(&self) -> bool {
        if let Some(raw) = self.raw {
            let state = raw.header().state.load();
            state.is_complete()
        } else {
            true
        }
    }

    /// Set the waker that is notified when the task completes.
    pub(crate) fn set_join_waker(&mut self, waker: &Waker) {
        if let Some(raw) = self.raw {
            if raw.try_set_join_waker(waker) {

Review comment (suggestion):
Adding a call to `time::pause` ensures that this test doesn't actually take a full second to run. The `#` hides it from the documentation.
```suggestion
    /// #[tokio::main]
    /// async fn main() {
    /// #  tokio::time::pause();
    ///    let handle1 = tokio::spawn(async {
    ///        // do some stuff here
```

Code after:
    ///    for handle in &handles {
    ///        handle.abort();
    ///    }
    ///
    ///    for handle in handles {
    ///        assert!(handle.await.unwrap_err().is_cancelled());
    ///    }
    /// }
    /// ```
    /// [cancelled]: method@super::error::JoinError::is_cancelled
    pub fn abort(&self) {
        if let Some(raw) = self.raw {
            raw.remote_abort();
        }
    }

    /// Checks if the task associated with this `JoinHandle` has finished.
    ///
    /// Please note that this method can return `false` even if [`abort`] has been
    /// called on the task. This is because the cancellation process may take
    /// some time, and this method does not return `true` until it has
    /// completed.
    ///
    /// ```rust
    /// use tokio::time;
    ///
    /// # #[tokio::main(flavor = "current_thread")]
    /// # async fn main() {
    /// # time::pause();
    /// let handle1 = tokio::spawn(async {
    ///     // do some stuff here
    /// });
    /// let handle2 = tokio::spawn(async {
    ///     // do some other stuff here
    ///     time::sleep(time::Duration::from_secs(10)).await;
    /// });
    /// // Wait for the task to finish
    /// handle2.abort();
    /// time::sleep(time::Duration::from_secs(1)).await;
    /// assert!(handle1.is_finished());
    /// assert!(handle2.is_finished());
    /// # }
    /// ```
    /// [`abort`]: method@JoinHandle::abort
    pub fn is_finished(&self) -> bool {
        if let Some(raw) = self.raw {
            let state = raw.header().state.load();
            state.is_complete()
        } else {
            true
        }