public abstract class AbstractResourceContainer extends Object implements ResourceContainer
ResourceContainer
that practices
the attach/detach paradigm described in ResourceContainer.run(Runnable)
.
Each ResourceContainer
implementation must inherit from this class.
ResourceContainer.run(Runnable)
ResourceContainer.State
Constructor and Description |
---|
AbstractResourceContainer() |
Modifier and Type | Method and Description |
---|---|
protected abstract void |
attach()
Attach to this resource container.
|
abstract void |
destroy()
Destroys this resource container, also kills the attached threads and releases
resources described in
ResourceContainer.getConstraints() . |
protected abstract void |
detach()
Detach from this resource container and return to root container.
|
abstract Iterable<Constraint> |
getConstraints()
Gets container's
Constraint s |
abstract ResourceContainer.State |
getState()
Returns the current ResourceContainer state.
|
void |
run(Runnable command)
Attach the current thread to the ResourceContainer to run the
command ,
and detach the ResourceContainer when command is either normally finished
or terminated by Exception. |
abstract void |
updateConstraint(Constraint constraint)
Updates
Constraint of this resource container. |
public void run(Runnable command)
ResourceContainer
command
,
and detach the ResourceContainer when command
is either normally finished
or terminated by Exception.
At the same time, it is not allowed to switch directly between any two
containers. If the switch is indeed required, the
ResourceContainer.root()
container should be used.
This way restricts the container attach/detach mode for the API users, but is less error-prone.
ResourceContainer resourceContainer = .... assert ResourceContainer.current() == ResourceContainer.root(); resourceContainer.run(() -> { assert ResourceContainer.current() == resourceContainer; }); assert ResourceContainer.current() == ResourceContainer.root();
run
in interface ResourceContainer
command
- the target codepublic abstract ResourceContainer.State getState()
ResourceContainer
getState
in interface ResourceContainer
public abstract void updateConstraint(Constraint constraint)
ResourceContainer
Constraint
of this resource container.updateConstraint
in interface ResourceContainer
constraint
- constraints listpublic abstract Iterable<Constraint> getConstraints()
ResourceContainer
Constraint
sgetConstraints
in interface ResourceContainer
Constraint
spublic abstract void destroy()
ResourceContainer
ResourceContainer.getConstraints()
.
Once this method is called, the state will become ResourceContainer.State.STOPPING
.
And the caller thread will be blocked until all the resources have been released.
Then the container state will become ResourceContainer.State.DEAD
.
destroy
in interface ResourceContainer
protected abstract void attach()
ResourceContainer.current()
as a root container
before calling this method.protected abstract void detach()
"Copyright 2018, Alibaba, Inc."