Package org.silverpeas.core.thread
Class ManagedThreadPool
- java.lang.Object
-
- org.silverpeas.core.thread.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
orCallable
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)
<V> Future<V>
invoke(Callable<V> callable)
Invokes the givenCallable
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 givenCallable
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)
<V> List<Future<V>>
invoke(List<? extends Callable<V>> callables, ManagedThreadPool.ExecutionConfig config)
Invokes the givenCallable
instances into managed threads.
EachCallable
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 invokingRunnable
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)
void
invokeAndAwaitTermination(List<? extends Runnable> runnables)
void
invokeAndAwaitTermination(List<? extends Runnable> runnables, ManagedThreadPool.ExecutionConfig config)
void
invokeAndAwaitTermination(Stream<? extends Runnable> runnables, ManagedThreadPool.ExecutionConfig config)
Invokes the given stream ofRunnable
instances into a managed thread and waiting for the end of execution of all of it.
EachRunnable
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.
-
-
-
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 givenRunnable
instances into a managed thread.
EachRunnable
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
- theRunnable
instances to invoke.- Returns:
- the list of threads that have been invoked.
-
invokeAndAwaitTermination
public void invokeAndAwaitTermination(Runnable... runnables) throws ManagedThreadPoolException
Invokes the givenRunnable
instances into a managed thread and waiting for the end of execution of all of it.
EachRunnable
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
- theRunnable
instances to invoke.- Throws:
ManagedThreadPoolException
- if the invocation fails.
-
invokeAndAwaitTermination
public void invokeAndAwaitTermination(List<? extends Runnable> runnables) throws ManagedThreadPoolException
Invokes the givenRunnable
instances into a managed thread and waiting for the end of execution of all of it.
EachRunnable
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
- theRunnable
instances to invoke.- Throws:
ManagedThreadPoolException
- if the invocation fails.
-
invokeAndAwaitTermination
public void invokeAndAwaitTermination(List<? extends Runnable> runnables, ManagedThreadPool.ExecutionConfig config) throws ManagedThreadPoolException
Invokes the givenRunnable
instances into a managed thread and waiting for the end of execution of all of it.
EachRunnable
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
- theRunnable
instances to invoke.config
- theRunnable
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 ofRunnable
instances into a managed thread and waiting for the end of execution of all of it.
EachRunnable
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 ofRunnable
instances to invoke.config
- theRunnable
instances execution configuration.- Throws:
ManagedThreadPoolException
- if the invocation fails.
-
invoke
public <V> Future<V> invoke(Callable<V> callable) throws InterruptedException
Invokes the givenCallable
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 aCallable
instance.- Parameters:
callable
- the callable to invoke.- Returns:
- the
Future
returned by the invocation of the givenCallable
instance. - Throws:
InterruptedException
- if interrupted while waiting
-
invoke
public <V> Future<V> invoke(Callable<V> callable, ManagedThreadPool.ExecutionConfig config) throws InterruptedException
Invokes the givenCallable
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 aCallable
instance.- Parameters:
callable
- the callable to invoke.config
- theCallable
instances execution configuration.- Returns:
- the
Future
returned by the invocation of the givenCallable
instance. - Throws:
InterruptedException
- if interrupted while waiting
-
invoke
public <V> List<Future<V>> invoke(List<? extends Callable<V>> callables) throws InterruptedException
Invokes the givenCallable
instances into managed threads.
EachCallable
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 aCallable
instance.- Parameters:
callables
- theCallable
instances to invoke.- Returns:
- the list of
Future
returned by the invocation of each givenCallable
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 givenCallable
instances into managed threads.
EachCallable
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 invokingRunnable
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 aCallable
instance.- Parameters:
callables
- theCallable
instances to invoke.config
- theCallable
instances execution configuration.- Returns:
- the list of
Future
returned by the invocation of each givenCallable
instances. - Throws:
InterruptedException
- if interrupted while waiting
-
-