Interface ThreadPool


  • public interface ThreadPool
    This class creates and contains thread pools which are used by Javacord.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      java.util.concurrent.ScheduledExecutorService getDaemonScheduler()
      Gets the used daemon scheduler.
      java.util.concurrent.ExecutorService getExecutorService()
      Gets the used executor service.
      java.util.concurrent.ScheduledExecutorService getScheduler()
      Gets the used scheduler.
      java.util.concurrent.ExecutorService getSingleDaemonThreadExecutorService​(java.lang.String threadName)
      Gets an executor service which only uses a single daemon thread.
      java.util.concurrent.ExecutorService getSingleThreadExecutorService​(java.lang.String threadName)
      Gets an executor service which only uses a single thread.
      java.util.Optional<java.util.concurrent.ExecutorService> removeAndShutdownSingleThreadExecutorService​(java.lang.String threadName)
      Removes an existing executor service.
      <T> java.util.concurrent.CompletableFuture<T> runAfter​(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> task, long duration, java.util.concurrent.TimeUnit unit)
      Executes code after a given duration.
    • Method Detail

      • getExecutorService

        java.util.concurrent.ExecutorService getExecutorService()
        Gets the used executor service.
        Returns:
        The used executor service.
      • getScheduler

        java.util.concurrent.ScheduledExecutorService getScheduler()
        Gets the used scheduler.
        Returns:
        The used scheduler.
      • getDaemonScheduler

        java.util.concurrent.ScheduledExecutorService getDaemonScheduler()
        Gets the used daemon scheduler.
        Returns:
        The used daemon scheduler.
      • getSingleThreadExecutorService

        java.util.concurrent.ExecutorService getSingleThreadExecutorService​(java.lang.String threadName)
        Gets an executor service which only uses a single thread.
        Parameters:
        threadName - The thread name of the executor service. Will create a new one if the thread name is used the first time.
        Returns:
        The executor service with the given thread name. Never null!
      • getSingleDaemonThreadExecutorService

        java.util.concurrent.ExecutorService getSingleDaemonThreadExecutorService​(java.lang.String threadName)
        Gets an executor service which only uses a single daemon thread.
        Parameters:
        threadName - The thread name of the executor service. Will create a new one if the thread name is used the first time.
        Returns:
        The executor service with the given thread name. Never null!
      • removeAndShutdownSingleThreadExecutorService

        java.util.Optional<java.util.concurrent.ExecutorService> removeAndShutdownSingleThreadExecutorService​(java.lang.String threadName)
        Removes an existing executor service.

        This allows you to get a fresh executor service when calling getSingleThreadExecutorService(String) again.

        Parameters:
        threadName - The thread name of the executor service.
        Returns:
        The removed and shutdown executor service with the given thread name.
      • runAfter

        <T> java.util.concurrent.CompletableFuture<T> runAfter​(java.util.function.Supplier<java.util.concurrent.CompletableFuture<T>> task,
                                                               long duration,
                                                               java.util.concurrent.TimeUnit unit)
        Executes code after a given duration.

        Tasks will be scheduled on the daemon executor, allowing the bot to shutdown without all tasks being executed. This method is not meant to persist scheduled task over multiple bot life cycles.

        Type Parameters:
        T - The return type of the future.
        Parameters:
        task - The code to run.
        duration - The duration to run the code after.
        unit - The unit of the duration given.
        Returns:
        A future that completes when the scheduled task is finished.