Package com.illumon.modelfarm
Class ModelFarmBase<DATATYPE>
java.lang.Object
com.illumon.modelfarm.ModelFarmBase<DATATYPE>
- Type Parameters:
DATATYPE
- data type
- All Implemented Interfaces:
ModelFarm
- Direct Known Subclasses:
ModelFarmOnDemand
,RDMModelFarm
public abstract class ModelFarmBase<DATATYPE> extends Object implements ModelFarm
A multithreaded resource to execute data driven models.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ModelFarmBase.GetDataLockType
Type of locking used when loading data from the data manager.static class
ModelFarmBase.State
Run state of the farm. -
Field Summary
-
Constructor Summary
Constructors Modifier Constructor Description protected
ModelFarmBase(int nThreads, Model<DATATYPE> model)
Create a multithreaded resource to execute data driven models. -
Method Summary
Modifier and Type Method Description boolean
awaitTermination()
Blocks until all tasks have completed execution after a shutdown request.boolean
awaitTermination(long timeout, TimeUnit unit)
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, whichever happens first.protected abstract void
execute()
Executes the next task in the work queue.protected static FunctionalInterfaces.ThrowingBiConsumer<com.illumon.modelfarm.ModelFarmBase.QueryDataRetrievalOperation,NotificationStepSource,RuntimeException>
getDoLockedConsumer(ModelFarmBase.GetDataLockType lockType)
Returns aThrowingConsumer
that takes aModelFarmBase.QueryDataRetrievalOperation
, acquires aLiveTableMonitor
lock based on the specifiedlockType
, then executes theFitDataPopulator
with the appropriate value for usePrev.protected ModelFarmBase.State
getState()
Gets the current run state of the model farm.protected abstract boolean
isQueueEmpty()
Returns true if the model farm queue is empty and false if the queue contains elements to execute.protected abstract void
modelFarmStarted()
Method called after the model farm threads have been started.void
shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.void
shutdownAndAwaitTermination()
Shuts down and then awaits termination.boolean
shutdownAndAwaitTermination(long timeout, TimeUnit unit)
Shuts down and then awaits termination.void
start()
Initiates execution.void
terminate()
Attempt to terminate the ModelFarm byshutting it down
and interrupting all worker threads.String
toString()
-
Field Details
-
model
The model to which data should be passed.
-
-
Constructor Details
-
ModelFarmBase
Create a multithreaded resource to execute data driven models.- Parameters:
nThreads
- number of worker threads.model
- model to execute.
-
-
Method Details
-
execute
Executes the next task in the work queue.- Throws:
InterruptedException
- if interrupted while executing
-
getDoLockedConsumer
protected static FunctionalInterfaces.ThrowingBiConsumer<com.illumon.modelfarm.ModelFarmBase.QueryDataRetrievalOperation,NotificationStepSource,RuntimeException> getDoLockedConsumer(ModelFarmBase.GetDataLockType lockType)Returns aThrowingConsumer
that takes aModelFarmBase.QueryDataRetrievalOperation
, acquires aLiveTableMonitor
lock based on the specifiedlockType
, then executes theFitDataPopulator
with the appropriate value for usePrev.- Parameters:
lockType
- The way of acquiring theLiveTableMonitor
lock.- Returns:
- A function that runs a
-
getState
Gets the current run state of the model farm. The state isnull
before the model farm has started.- Returns:
- current run state of the model farm.
-
start
public final void start()Description copied from interface:ModelFarm
Initiates execution. -
modelFarmStarted
protected abstract void modelFarmStarted()Method called after the model farm threads have been started. Implementing classes can override this to perform additional setup (e.g. creating and starting listeners). The default implementation does nothing. -
shutdown
public final void shutdown()Description copied from interface:ModelFarm
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.This method does not wait for previously submitted tasks to complete execution. Use
awaitTermination
to do that. -
terminate
public final void terminate()Attempt to terminate the ModelFarm byshutting it down
and interrupting all worker threads. -
awaitTermination
public final boolean awaitTermination()Description copied from interface:ModelFarm
Blocks until all tasks have completed execution after a shutdown request.- Specified by:
awaitTermination
in interfaceModelFarm
- Returns:
true
if this executor terminated andfalse
if the timeout elapsed before termination
-
awaitTermination
Description copied from interface:ModelFarm
Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, whichever happens first.- Specified by:
awaitTermination
in interfaceModelFarm
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Returns:
true
if this executor terminated andfalse
if the timeout elapsed before termination
-
shutdownAndAwaitTermination
public final void shutdownAndAwaitTermination()Description copied from interface:ModelFarm
Shuts down and then awaits termination.- Specified by:
shutdownAndAwaitTermination
in interfaceModelFarm
-
shutdownAndAwaitTermination
Description copied from interface:ModelFarm
Shuts down and then awaits termination.- Specified by:
shutdownAndAwaitTermination
in interfaceModelFarm
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of the timeout argument- Returns:
true
if this executor terminated andfalse
if the timeout elapsed before termination
-
isQueueEmpty
protected abstract boolean isQueueEmpty()Returns true if the model farm queue is empty and false if the queue contains elements to execute.- Returns:
- true if the model farm queue is empty and false if the queue contains elements to execute.
-
toString
-