Class ManagedThreadPool


  • @Technical
    @Bean
    @Singleton
    public class ManagedThreadPool
    extends Object
    A pool of threads that are managed by the underlying application server in which runs Silverpeas. This pool manages the life-cycle of threads and distributes over them the different tasks that are passed to it.

    This useful managed thread pool permits to invoke instances of Runnable or Callable by using the managed thread pools provided by the application server.

    Author:
    Yohann Chastagnier
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  ManagedThreadPool.ExecutionConfig
      Class that permits to specify the execution configuration.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected ManagedThreadPool()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ManagedThreadPool getPool()
      Gets a pool of managed threads.
      List<Thread> invoke​(Runnable... runnables)
      Invokes the given Runnable instances into a managed thread.
      Each Runnable instance will be used one managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      <V> Future<V> invoke​(Callable<V> callable)
      Invokes the given Callable instance into a managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      <V> Future<V> invoke​(Callable<V> callable, ManagedThreadPool.ExecutionConfig config)
      Invokes the given Callable instance into a managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      <V> List<Future<V>> invoke​(List<? extends Callable<V>> callables)
      Invokes the given Callable instances into managed threads.
      Each Callable instance will be used one managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      <V> List<Future<V>> invoke​(List<? extends Callable<V>> callables, ManagedThreadPool.ExecutionConfig config)
      Invokes the given Callable instances into managed threads.
      Each Callable instance will be used one managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      A difference with invoking Runnable instances is that if a timeout is set the caller will get back the hand after the successful execution of all threads or after the timeout, but never before.
      void invokeAndAwaitTermination​(Runnable... runnables)
      Invokes the given Runnable instances into a managed thread and waiting for the end of execution of all of it.
      Each Runnable instance will be used one managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      void invokeAndAwaitTermination​(List<? extends Runnable> runnables)
      Invokes the given Runnable instances into a managed thread and waiting for the end of execution of all of it.
      Each Runnable instance will be used one managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      void invokeAndAwaitTermination​(List<? extends Runnable> runnables, ManagedThreadPool.ExecutionConfig config)
      Invokes the given Runnable instances into a managed thread and waiting for the end of execution of all of it.
      Each Runnable instance will be used one managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
      void invokeAndAwaitTermination​(Stream<? extends Runnable> runnables, ManagedThreadPool.ExecutionConfig config)
      Invokes the given stream of Runnable instances into a managed thread and waiting for the end of execution of all of it.
      Each Runnable instance in the stream will be used one managed thread.
      If the application server has no more thread to supply, then the execution will wait until it exists one again available.
    • Constructor Detail

      • ManagedThreadPool

        protected ManagedThreadPool()
    • Method Detail

      • getPool

        public static ManagedThreadPool getPool()
        Gets a pool of managed threads.
        Returns:
        a ManagedThreadPool instance ready to take in charge the passed executions in different the threads of the pool.
      • invoke

        public List<Thread> invoke​(Runnable... runnables)
        Invokes the given Runnable instances into a managed thread.
        Each Runnable instance will be used one managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Parameters:
        runnables - the Runnable instances to invoke.
        Returns:
        the list of threads that have been invoked.
      • invokeAndAwaitTermination

        public void invokeAndAwaitTermination​(Runnable... runnables)
                                       throws ManagedThreadPoolException
        Invokes the given Runnable instances into a managed thread and waiting for the end of execution of all of it.
        Each Runnable instance will be used one managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Parameters:
        runnables - the Runnable instances to invoke.
        Throws:
        ManagedThreadPoolException - if the invocation fails.
      • invokeAndAwaitTermination

        public void invokeAndAwaitTermination​(List<? extends Runnable> runnables)
                                       throws ManagedThreadPoolException
        Invokes the given Runnable instances into a managed thread and waiting for the end of execution of all of it.
        Each Runnable instance will be used one managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Parameters:
        runnables - the Runnable instances to invoke.
        Throws:
        ManagedThreadPoolException - if the invocation fails.
      • invokeAndAwaitTermination

        public void invokeAndAwaitTermination​(List<? extends Runnable> runnables,
                                              ManagedThreadPool.ExecutionConfig config)
                                       throws ManagedThreadPoolException
        Invokes the given Runnable instances into a managed thread and waiting for the end of execution of all of it.
        Each Runnable instance will be used one managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Parameters:
        runnables - the Runnable instances to invoke.
        config - the Runnable instances execution configuration.
        Throws:
        ManagedThreadPoolException - if the invocation fails.
      • invokeAndAwaitTermination

        public void invokeAndAwaitTermination​(Stream<? extends Runnable> runnables,
                                              ManagedThreadPool.ExecutionConfig config)
                                       throws ManagedThreadPoolException
        Invokes the given stream of Runnable instances into a managed thread and waiting for the end of execution of all of it.
        Each Runnable instance in the stream will be used one managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Parameters:
        runnables - a stream of Runnable instances to invoke.
        config - the Runnable instances execution configuration.
        Throws:
        ManagedThreadPoolException - if the invocation fails.
      • invoke

        public <V> Future<V> invoke​(Callable<V> callable)
                             throws InterruptedException
        Invokes the given Callable instance into a managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Type Parameters:
        V - the type of the returned value of a Callable instance.
        Parameters:
        callable - the callable to invoke.
        Returns:
        the Future returned by the invocation of the given Callable instance.
        Throws:
        InterruptedException - if interrupted while waiting
      • invoke

        public <V> Future<V> invoke​(Callable<V> callable,
                                    ManagedThreadPool.ExecutionConfig config)
                             throws InterruptedException
        Invokes the given Callable instance into a managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Type Parameters:
        V - the type of the returned value of a Callable instance.
        Parameters:
        callable - the callable to invoke.
        config - the Callable instances execution configuration.
        Returns:
        the Future returned by the invocation of the given Callable instance.
        Throws:
        InterruptedException - if interrupted while waiting
      • invoke

        public <V> List<Future<V>> invoke​(List<? extends Callable<V>> callables)
                                   throws InterruptedException
        Invokes the given Callable instances into managed threads.
        Each Callable instance will be used one managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        Type Parameters:
        V - the type of the returned value of a Callable instance.
        Parameters:
        callables - the Callable instances to invoke.
        Returns:
        the list of Future returned by the invocation of each given Callable instances.
        Throws:
        InterruptedException - if interrupted while waiting
      • invoke

        public <V> List<Future<V>> invoke​(List<? extends Callable<V>> callables,
                                          ManagedThreadPool.ExecutionConfig config)
                                   throws InterruptedException
        Invokes the given Callable instances into managed threads.
        Each Callable instance will be used one managed thread.
        If the application server has no more thread to supply, then the execution will wait until it exists one again available.
        A difference with invoking Runnable instances is that if a timeout is set the caller will get back the hand after the successful execution of all threads or after the timeout, but never before.
        Type Parameters:
        V - the type of the returned value of a Callable instance.
        Parameters:
        callables - the Callable instances to invoke.
        config - the Callable instances execution configuration.
        Returns:
        the list of Future returned by the invocation of each given Callable instances.
        Throws:
        InterruptedException - if interrupted while waiting