public abstract class ForkInvoker<T> extends Object
invoke(Object)
into calls to fork(Object)
or call(Object)
depending on the max number of reentrant calls to invoke(Object)
.
This class prevents StackOverflowError
s in case of methods that end up invoking themselves,
such is common for Callback.succeeded()
.
Typical use case is:
public void reentrantMethod(Object param) { if (condition || tooManyReenters) fork(param) else call(param) }Calculating
tooManyReenters
usually involves using a ThreadLocal
and algebra on the
number of reentrant invocations, which is factored out in this class for convenience.
The same code using this class becomes:
private final ForkInvoker invoker = ...; public void reentrantMethod(Object param) { invoker.invoke(param); }
Constructor and Description |
---|
ForkInvoker(int maxInvocations)
Creates an instance with the given max number of reentrant calls to
invoke(Object)
If maxInvocations is zero or negative, it is interpreted
as if the max number of reentrant calls is infinite. |
Modifier and Type | Method and Description |
---|---|
abstract void |
call(T arg)
Executes the direct, non-forked, invocation
|
protected boolean |
condition()
Subclasses should override this method returning true if they want
invoke(Object) to call fork(Object) . |
abstract void |
fork(T arg)
Executes the forked invocation
|
boolean |
invoke(T arg)
Invokes either
fork(Object) or call(Object) . |
public ForkInvoker(int maxInvocations)
invoke(Object)
If maxInvocations
is zero or negative, it is interpreted
as if the max number of reentrant calls is infinite.maxInvocations
- the max number of reentrant calls to invoke(Object)
public boolean invoke(T arg)
fork(Object)
or call(Object)
.
If condition()
returns true, fork(Object)
is invoked.
Otherwise, if the max number of reentrant calls is positive and the
actual number of reentrant invocations exceeds it, fork(Object)
is invoked.
Otherwise, call(Object)
is invoked.arg
- TODOfork(Object)
has been called, false otherwiseprotected boolean condition()
invoke(Object)
to call fork(Object)
.invoke(Object)
should call fork(Object)
, false otherwisepublic abstract void fork(T arg)
arg
- TODOpublic abstract void call(T arg)
arg
- TODOCopyright © 1995-2013 Mort Bay Consulting. All Rights Reserved.