Package org.nustaq.kontraktor.impl
Class DispatcherThread
java.lang.Object
java.lang.Thread
org.nustaq.kontraktor.impl.DispatcherThread
- All Implemented Interfaces:
java.lang.Runnable,Monitorable
public class DispatcherThread extends java.lang.Thread implements Monitorable
Implements the default dispatcher/scheduling of actors.
For each actor created from "outside" (not from within another actor). A new DispatcherThread is created
automatically. An actor created from within another actor inherits the dispatcher of the enclosing actor
by default.
Calls from actors sharing the same dispatcher are done directly (no queueing). Calls across actors in
different dispatchers are put to the Channel of the receiving dispatcher. Note that cross-dispatcher calls
are like 1000 times slower than inbound calls.
Each dispatcher owns exactly one single thread.
Note that dispatchers must be terminated if not needed any longer, as a thread is associated with them.
For more sophisticated applications it might be appropriate to manually set up dispatchers (Actors.assignDispatcher()).
The Actors.Channel method allows to specifiy a dedicated dispatcher on which to run the actor. This way it is possible
to exactly balance and control the number of threads created and which thread operates a set of actors.
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDispatcherThread.DispatcherReport -
Field Summary
Fields Modifier and Type Field Description java.util.ArrayList__stackstatic java.util.concurrent.atomic.AtomicIntegeractiveDispatchersprotected booleanautoShutDownstatic booleanDUMP_CATCHEDstatic intMILLIS_AFTER_CREATION_BEFORE_REBALANCINGstatic intPOLL_ALL_Qstatic intPOLL_CB_Qstatic intQUEUE_PERCENTAGE_TRIGGERING_REBALANCEstatic intSCHEDULE_TICK_NANOSprotected booleanshutDown -
Constructor Summary
Constructors Constructor Description DispatcherThread(Scheduler scheduler)DispatcherThread(Scheduler scheduler, boolean autoShutDown) -
Method Summary
Modifier and Type Method Description voidaddActor(Actor act)intgetAccumulatedQSizes()accumulated queue sizes of all actorsActor[]getActors()intgetLoad()WARNING: call rarely, this method might have O(n) runtime with default unbounded queues.intgetQSize()IPromisegetReport()SchedulergetScheduler()IPromise<Monitorable[]>getSubMonitorables()booleanisAutoShutDown()booleanisEmpty()booleanisIsolated()booleanisShutDown()booleanpollQs()booleanpollQs(int queues)booleanpollQs(Actor[] actors, int queues)protected CallEntrypollQueues(Actor[] actors, int queues)voidrun()voidschedulePendingAdds()add actors which have been marked to be scheduled on thisbooleanschedules(java.lang.Object receiverRef)can be called from the dispacther thread itself onlyvoidsetAutoShutDown(boolean autoShutDown)voidsetIsolated(boolean isIsolated)voidshutDown()terminate operation after emptying QvoidshutDownImmediate()terminate operation immediately.java.lang.StringtoString()voidwaitEmpty(long nanos)blocking method, use for debugging only.Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, yield
-
Field Details
-
DUMP_CATCHED
public static boolean DUMP_CATCHED -
SCHEDULE_TICK_NANOS
public static int SCHEDULE_TICK_NANOS -
QUEUE_PERCENTAGE_TRIGGERING_REBALANCE
public static int QUEUE_PERCENTAGE_TRIGGERING_REBALANCE -
MILLIS_AFTER_CREATION_BEFORE_REBALANCING
public static int MILLIS_AFTER_CREATION_BEFORE_REBALANCING -
activeDispatchers
public static java.util.concurrent.atomic.AtomicInteger activeDispatchers -
POLL_ALL_Q
public static final int POLL_ALL_Q- See Also:
- Constant Field Values
-
POLL_CB_Q
public static final int POLL_CB_Q- See Also:
- Constant Field Values
-
shutDown
protected boolean shutDown -
__stack
public java.util.ArrayList __stack -
autoShutDown
protected volatile boolean autoShutDown
-
-
Constructor Details
-
Method Details
-
toString
public java.lang.String toString()- Overrides:
toStringin classjava.lang.Thread
-
isIsolated
public boolean isIsolated() -
setIsolated
public void setIsolated(boolean isIsolated) -
addActor
-
isAutoShutDown
public boolean isAutoShutDown() -
setAutoShutDown
public void setAutoShutDown(boolean autoShutDown) -
run
public void run()- Specified by:
runin interfacejava.lang.Runnable- Overrides:
runin classjava.lang.Thread
-
schedulePendingAdds
public void schedulePendingAdds()add actors which have been marked to be scheduled on this -
pollQueues
-
pollQs
public boolean pollQs()- Returns:
- false if no message could be polled
-
pollQs
public boolean pollQs(int queues) -
pollQs
- Parameters:
queues- - see constants- Returns:
- false if no message could be polled
-
getLoad
public int getLoad()WARNING: call rarely, this method might have O(n) runtime with default unbounded queues.- Returns:
- percentage of queue fill of max actor
-
getAccumulatedQSizes
public int getAccumulatedQSizes()accumulated queue sizes of all actors- Returns:
-
getQSize
public int getQSize()- Returns:
- accumulated q size of all dispatched actors
-
isShutDown
public boolean isShutDown()- Returns:
- true if DispatcherThread is shut down
-
shutDown
public void shutDown()terminate operation after emptying Q -
shutDownImmediate
public void shutDownImmediate()terminate operation immediately. Pending messages in Q are lost -
isEmpty
public boolean isEmpty() -
waitEmpty
public void waitEmpty(long nanos)blocking method, use for debugging only. -
getScheduler
-
getActors
- Returns:
- a copy of actors used
-
schedules
public boolean schedules(java.lang.Object receiverRef)can be called from the dispacther thread itself only- Parameters:
receiverRef-- Returns:
-
getReport
- Specified by:
getReportin interfaceMonitorable
-
getSubMonitorables
- Specified by:
getSubMonitorablesin interfaceMonitorable
-