T
- the type of the task resultpublic class SandboxBackgroundTask<T>
extends java.lang.Object
SandboxBackgroundTask
allows a sandbox related task to be
performed in the background using a SandboxWorker
object.
Part of the task may be performed in the current thread first before the
rest of the task is performed in the background thread. Subclasses may
override the "fixture methods" to execute custom code before or after the
task, or when an exception is thrown.Modifier and Type | Class and Description |
---|---|
static class |
SandboxBackgroundTask.Job<R>
A part of the task.
|
static class |
SandboxBackgroundTask.Proxy<R>
An object that performs a task using a
SandboxBackgroundTask
object. |
static class |
SandboxBackgroundTask.SimpleProxy<R>
A simple proxy that allows clients to wait for the task result.
|
static class |
SandboxBackgroundTask.SimpleTask<R>
A task created by
SimpleProxy . |
Modifier and Type | Field and Description |
---|---|
protected SandboxBackgroundTask.Job<T> |
backgroundJob
The job that runs in a background thread.
|
protected SandboxBackgroundTask.Job<java.lang.Object> |
foregroundJob
The job that runs in the current thread, may be
null . |
Constructor and Description |
---|
SandboxBackgroundTask(SandboxBackgroundTask.Job<java.lang.Object> foregroundJob,
SandboxBackgroundTask.Job<T> backgroundJob)
Creates a task.
|
SandboxBackgroundTask(SandboxBackgroundTask.Job<T> backgroundJob)
Creates a task that is performed entirely in a background thread.
|
Modifier and Type | Method and Description |
---|---|
protected void |
catchBlock(java.lang.Exception e)
Invoked when a foreground job or background job throws an exception, or
when
tryBlockInitialization or tryBlockFinalization
throws an exception. |
protected void |
catchBlock(java.lang.Throwable e)
Invoked when a foreground job or background job throws an error, or
when
tryBlockInitialization or tryBlockFinalization
throws an error. |
protected void |
finallyBlock()
Invoked after an exception is thrown, or after
tryBlockFinalization completes without any exception. |
protected <X> SandboxWorker<X> |
newSandboxWorker(java.util.concurrent.Callable<X> job)
Creates a
SandboxWorker object to run the background job. |
void |
perform()
Performs this task.
|
void |
perform(long bgTimeout,
java.util.concurrent.TimeUnit unit)
Performs this task.
|
protected void |
tryBlockFinalization(T result)
Invoked in a background thread after the task is performed.
|
protected void |
tryBlockInitialization()
Invoked before a task is performed.
|
protected void |
unhandledThrowable(java.lang.Throwable t)
Invoked when
catchBlock or finallyBlock throws
an exception. |
protected final SandboxBackgroundTask.Job<java.lang.Object> foregroundJob
null
.protected final SandboxBackgroundTask.Job<T> backgroundJob
public SandboxBackgroundTask(SandboxBackgroundTask.Job<java.lang.Object> foregroundJob, SandboxBackgroundTask.Job<T> backgroundJob)
foregroundJob
- the job that runs in the current thread;
null
if the whole task is to be
performed in the background. If specified, the input
to the foreground job is always null. The output of
the foreground job is passed into the background job
as the input. The background job must complete the
task and return the task result.backgroundJob
- the job that runs in a background threadjava.lang.IllegalArgumentException
- if the background job is nullpublic SandboxBackgroundTask(SandboxBackgroundTask.Job<T> backgroundJob)
backgroundJob
- the job that runs in a background threadjava.lang.IllegalArgumentException
- if the given argument is nullpublic final void perform()
Note that any java.lang.Throwable
thrown by the jobs will not
be re-thrown by this method. java.lang.Exception
objects
should be handled by catchBlock(Exception)
and everything
else by unhandledThrowable(Throwable)
.
public final void perform(long bgTimeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException, java.util.concurrent.TimeoutException
TimeoutException
is thrown if the background job is still not done when the wait timed out.
Note that any java.lang.Throwable
thrown by the jobs will not
be re-thrown by this method. java.lang.Exception
objects
should be handled by catchBlock(Exception)
and everything
else by unhandledThrowable(Throwable)
.
bgTimeout
- the maximum time to wait