org.omnaest.utils.threads
Class FutureTaskManager

java.lang.Object
  extended by org.omnaest.utils.threads.FutureTaskManager
All Implemented Interfaces:
Serializable

public class FutureTaskManager
extends Object
implements Serializable

A FutureTaskManager will manage the Futures created e.g. by a ExecutorService.submit(java.util.concurrent.Callable) call and allows to wait on all managed Futures.

If an ExecutorService instance is declared using FutureTaskManager(ExecutorService), it is possible to let the FutureTaskManager submit tasks by invoking submitAndManage(Callable) or submitAndManage(Runnable).

Author:
Omnaest
See Also:
Serialized Form

Field Summary
protected  ExecutorService executorService
           
protected  List<Future<?>> futureList
           
 
Constructor Summary
FutureTaskManager()
          Does not support submitAndManage(Callable) and submitAndManage(Runnable).
FutureTaskManager(ExecutorService executorService)
           
 
Method Summary
 boolean areAllTasksFinished()
          Returns true if all given Future task return true for Future.isDone()
 void clearFinishedTasks()
          Clears all Futures which are Future.isDone()
 List<Future<?>> getFutureList()
          Returns the List of manage Futures
 boolean hasExecutorService()
          Returns true if this FutureTaskManager has an ExecutorService available
 void manageFutureTask(Future<?> future)
          Manages the given Future
 void manageFutureTask(Iterable<Future<?>> futureIterable)
          Manages the given Future instances
 void submitAndManage(Callable<?> callable)
          Submits the given Callable to the internal ExecutorService.
 void submitAndManage(Callable<?> callable, int submitCount)
          Similar to submitAndManage(Callable) but the Callable will be submitted the given number times
 void submitAndManage(ExecutorService executorService, Callable<?> callable)
          Submits the given Callable to the given ExecutorService instance and calls manageFutureTask(Future) for the resulting FutureTask
 void submitAndManage(ExecutorService executorService, Callable<?> callable, int submitCount)
           
 void submitAndManage(ExecutorService executorService, Runnable runnable)
          Submits the given Runnable to the given ExecutorService instance and calls manageFutureTask(Future) for the resulting FutureTask
 void submitAndManage(ExecutorService executorService, Runnable runnable, int submitCount)
           
 void submitAndManage(Runnable runnable)
           
 void submitAndManage(Runnable runnable, int submitCount)
          Similar to submitAndManage(Callable, int) but for Runnable
 void submitAndManageCallables(ExecutorService executorService, Collection<? extends Callable<?>> callableCollection, int submitCount)
           
 void submitAndManageRunnables(ExecutorService executorService, Collection<Runnable> runnableCollection, int submitCount)
           
<V> ExceptionHandledResult<List<V>>
waitForAllTasksToFinish()
          Uses the Future.get() to wait on all managed Futures until they are finished
static
<V> ExceptionHandledResult<V>
waitForTaskToFinish(Future<V> future)
          Waits for a given Future to finish.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

futureList

protected final List<Future<?>> futureList

executorService

protected final ExecutorService executorService
Constructor Detail

FutureTaskManager

public FutureTaskManager(ExecutorService executorService)
Parameters:
executorService -
See Also:
FutureTaskManager

FutureTaskManager

public FutureTaskManager()
Does not support submitAndManage(Callable) and submitAndManage(Runnable). If these methods should be used provide an ExecutorService instance using FutureTaskManager(ExecutorService)

See Also:
FutureTaskManager
Method Detail

manageFutureTask

public void manageFutureTask(Future<?> future)
Manages the given Future

Parameters:
future -

manageFutureTask

public void manageFutureTask(Iterable<Future<?>> futureIterable)
Manages the given Future instances

Parameters:
futureIterable -

submitAndManage

public void submitAndManage(ExecutorService executorService,
                            Callable<?> callable)
Submits the given Callable to the given ExecutorService instance and calls manageFutureTask(Future) for the resulting FutureTask

Parameters:
executorService -
callable -

submitAndManage

public void submitAndManage(ExecutorService executorService,
                            Runnable runnable)
Submits the given Runnable to the given ExecutorService instance and calls manageFutureTask(Future) for the resulting FutureTask

Parameters:
executorService -
runnable -

submitAndManage

public void submitAndManage(Callable<?> callable)
Submits the given Callable to the internal ExecutorService. Throws an UnsupportedOperationException if not ExecutorService instance is available.

Parameters:
callable -
See Also:
Callable

submitAndManage

public void submitAndManage(Callable<?> callable,
                            int submitCount)
Similar to submitAndManage(Callable) but the Callable will be submitted the given number times

Parameters:
callable -
submitCount -

submitAndManage

public void submitAndManage(Runnable runnable)
Parameters:
runnable -
See Also:
Runnable, submitAndManage(Callable)

submitAndManage

public void submitAndManage(Runnable runnable,
                            int submitCount)
Similar to submitAndManage(Callable, int) but for Runnable

Parameters:
runnable -
submitCount -

submitAndManage

public void submitAndManage(ExecutorService executorService,
                            Runnable runnable,
                            int submitCount)
Parameters:
executorService -
runnable -
submitCount -
See Also:
submitAndManage(Callable), submitAndManageRunnables(ExecutorService, Collection, int)

submitAndManage

public void submitAndManage(ExecutorService executorService,
                            Callable<?> callable,
                            int submitCount)
Parameters:
executorService -
callable -
submitCount -
See Also:
submitAndManage(ExecutorService, Callable), submitAndManageCallables(ExecutorService, Collection, int)

submitAndManageRunnables

public void submitAndManageRunnables(ExecutorService executorService,
                                     Collection<Runnable> runnableCollection,
                                     int submitCount)
Parameters:
executorService -
runnableCollection -
submitCount -
See Also:
submitAndManageCallables(ExecutorService, Collection, int)

submitAndManageCallables

public void submitAndManageCallables(ExecutorService executorService,
                                     Collection<? extends Callable<?>> callableCollection,
                                     int submitCount)
Parameters:
callableCollection -
submitCount -
See Also:
submitAndManage(ExecutorService, Callable), submitAndManage(Callable)

waitForAllTasksToFinish

public <V> ExceptionHandledResult<List<V>> waitForAllTasksToFinish()
Uses the Future.get() to wait on all managed Futures until they are finished

Returns:
ExceptionHandledResult

waitForTaskToFinish

public static <V> ExceptionHandledResult<V> waitForTaskToFinish(Future<V> future)
Waits for a given Future to finish. Returns an ExceptionHandledResult which does exclude any InterruptedException

Parameters:
future -
Returns:
ExceptionHandledResult

clearFinishedTasks

public void clearFinishedTasks()
Clears all Futures which are Future.isDone()


getFutureList

public List<Future<?>> getFutureList()
Returns the List of manage Futures

Returns:

hasExecutorService

public boolean hasExecutorService()
Returns true if this FutureTaskManager has an ExecutorService available

Returns:

areAllTasksFinished

public boolean areAllTasksFinished()
Returns true if all given Future task return true for Future.isDone()

Returns:


Copyright © 2013. All Rights Reserved.