|
org.openide.util 8.1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.openide.util.RequestProcessor
public final class RequestProcessor
Request processor is Executor (since version 7.16) capable to
perform asynchronous requests in a dedicated thread pool.
There are several use cases for RequestProcessor,
most of them start with creating own RequestProcessor
instance (which by itself is quite lightweight).
RequestProcessor and post tasks to it.
private static final RequestProcessor RP = new RequestProcessor("My tasks");
// later
RP.post(runnable, delay)
The above example guarantees that there is at most one runnable being
processed in parallel. All your requests are serialized and processed
one by one. RP works here like a simple mutex.
In case you want more tasks to run in parallel (not very often use case)
you can specify higher
throughput via RequestProcessor(java.lang.String, int). Then
the RP works like a queue of requests passing through a
semafore with predefined number of DOWN()s.
You can wait for your tasks to be processed by keeping a reference to the
last one and using waitFinished():
private static final RequestProcessor RP = new RequestProcessor("My tasks");
private volatile RequestProcessor.Task last;
// when posting update the task
last = RP.post(runnable, delay)
// later wait
last.waitFinished()
schedule method:
class Periodic implements Runnable {
private static final RequestProcessor RP = new RequestProcessor("My tasks");
private final RequestProcessor.Task CLEANER = RP.create(this);
public void run() {
doTheWork();
CLEANER.schedule(DELAY);
}
}
Please think twice before using such periodic
background activity. It is generally considered evil if some code runs
without any user action. Your code shall respect the application's state,
and for example when the application is minimized, do nothing.
class Updater implements PropertyChangeListener, Runnable {
private static final RequestProcessor RP = new RequestProcessor("My tasks");
private final RequestProcessor.Task UPDATE = RP.create(this);
public void propertyChange(PropertyChangeEvent ev) {
UPDATE.schedule(1000);
}
public void run() {
doTheWork();
}
}
The above code coaleases all events that arrive in 1s and for all of them
does doTheWork just once.
RequestProcessor.Task.cancel()
but if the task's run() method was already running, one was out of luck.
Since version 6.3
the thread running the task is interrupted and the Runnable can check for that
and terminate its execution sooner. In the runnable one shall check for
thread interruption (done from RequestProcessor.Task.cancel()) and
if true, return immediatelly as in this example:
private static final RequestProcessor RP = new RequestProcessor("Interruptible", 1, true);
public void run () {
while (veryLongTimeLoop) {
doAPieceOfIt ();
if (Thread.interrupted ()) return;
}
}
| Nested Class Summary | |
|---|---|
class |
RequestProcessor.Task
The task describing the request sent to the processor. |
| Constructor Summary | |
|---|---|
RequestProcessor()
Creates new RequestProcessor with automatically assigned unique name. |
|
RequestProcessor(String name)
Creates a new named RequestProcessor with throughput 1. |
|
RequestProcessor(String name,
int throughput)
Creates a new named RequestProcessor with defined throughput. |
|
RequestProcessor(String name,
int throughput,
boolean interruptThread)
Creates a new named RequestProcessor with defined throughput which can support interruption of the thread the processor runs in. |
|
RequestProcessor(String name,
int throughput,
boolean interruptThread,
boolean enableStackTraces)
Creates a new named RequestProcessor that allows to disable stack trace filling. |
|
| Method Summary | |
|---|---|
RequestProcessor.Task |
create(Runnable run)
Creates request that can be later started by setting its delay. |
RequestProcessor.Task |
create(Runnable run,
boolean initiallyFinished)
Creates request that can be later started by setting its delay. |
static RequestProcessor.Task |
createRequest(Runnable run)
Deprecated. Sharing of one singlethreaded RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases. |
void |
execute(Runnable command)
Implements contract of Executor. |
static RequestProcessor |
getDefault()
The getter for the shared instance of the RequestProcessor. |
boolean |
isRequestProcessorThread()
Tests if the current thread is request processor thread. |
RequestProcessor.Task |
post(Runnable run)
This methods asks the request processor to start given runnable immediately. |
RequestProcessor.Task |
post(Runnable run,
int timeToWait)
This methods asks the request processor to start given runnable after timeToWait milliseconds. |
RequestProcessor.Task |
post(Runnable run,
int timeToWait,
int priority)
This methods asks the request processor to start given runnable after timeToWait milliseconds. |
static RequestProcessor.Task |
postRequest(Runnable run)
Deprecated. Sharing of one singlethreaded RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases. |
static RequestProcessor.Task |
postRequest(Runnable run,
int timeToWait)
Deprecated. Sharing of one singlethreaded RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases. |
static RequestProcessor.Task |
postRequest(Runnable run,
int timeToWait,
int priority)
Deprecated. Sharing of one singlethreaded RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases. |
void |
stop()
Stops processing of runnables processor. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public RequestProcessor()
public RequestProcessor(String name)
name - the name to use for the request processor thread
public RequestProcessor(String name,
int throughput)
name - the name to use for the request processor threadthroughput - the maximal count of requests allowed to run in parallel
public RequestProcessor(String name,
int throughput,
boolean interruptThread)
RequestProcessor.Task.cancel()
but if the task was already running, one was out of luck. With this
constructor one can create a RequestProcessor which threads
thread running tasks are interrupted and the Runnable can check for that
and terminate its execution sooner. In the runnable one shall check for
thread interruption (done from RequestProcessor.Task.cancel()) and
if true, return immediatelly as in this example:
public void run () {
while (veryLongTimeLook) {
doAPieceOfIt ();
if (Thread.interrupted ()) return;
}
}
name - the name to use for the request processor threadthroughput - the maximal count of requests allowed to run in parallelinterruptThread - true if RequestProcessor.Task.cancel() shall interrupt the thread
public RequestProcessor(String name,
int throughput,
boolean interruptThread,
boolean enableStackTraces)
RequestProcessor that allows to disable stack trace filling.
By default, when assertions are on, each task posted on RequestProcessor stores
the stack trace at the time of posting. When an exception is later thrown from the task,
it allows to print not only stack trace of the task but also stack trace of the code that posted it.
However this may be a performance bottleneck in cases when hundreds of short task are scheduled.
This constructor then allows to create RequestProcessor which never stores stack traces
at the time of posting.
See constructor RequestProcessor(String, int, boolean) for details of interruptThread
parameter.
name - the name to use for the request processor threadthroughput - the maximal count of requests allowed to run in parallelinterruptThread - true if RequestProcessor.Task.cancel() shall interrupt the threadenableStackTraces - false when request processor should not fill stack traces when task is posted.
Default is true when assertions are enabled, false otherwise.| Method Detail |
|---|
public static RequestProcessor getDefault()
RequestProcessor.
This instance is shared by anybody who
needs a way of performing sporadic or repeated asynchronous work.
Tasks posted to this instance may be canceled until they start their
execution. If a there is a need to cancel a task while it is running
a seperate request processor needs to be created via
RequestProcessor(String, int, boolean) constructor.
RequestProcessor(String, int, boolean),
RequestProcessor.Task.cancel()public void execute(Runnable command)
Executor.
Simply delegates to post(java.lang.Runnable).
execute in interface Executorcommand - the runnable to executepublic RequestProcessor.Task post(Runnable run)
Thread.MIN_PRIORITY.
run - class to run
public RequestProcessor.Task post(Runnable run,
int timeToWait)
timeToWait milliseconds. The default priority is Thread.MIN_PRIORITY.
run - class to runtimeToWait - to wait before execution
public RequestProcessor.Task post(Runnable run,
int timeToWait,
int priority)
timeToWait milliseconds. Given priority is assigned to the
request. For request relaying please consider:
post(run, timeToWait, Thread.currentThread().getPriority());
run - class to runtimeToWait - to wait before executionpriority - the priority from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY
public RequestProcessor.Task create(Runnable run)
!isFinished() so doing waitFinished() will
block on and wait until the task is scheduled.
run - action to run in the process
public RequestProcessor.Task create(Runnable run,
boolean initiallyFinished)
run - action to run in the processinitiallyFinished - should the task be marked initially finished? If
so the RequestProcessor.Task.waitFinished() on the task will succeeded immediatelly even
the task has not yet been RequestProcessor.Task.schedule(int)d.
public boolean isRequestProcessorThread()
waitFinished method. Any two tasks created
by request processor must not wait for themself.
true if the current thread is request processor
thread, otherwise falsepublic void stop()
@Deprecated public static RequestProcessor.Task postRequest(Runnable run)
RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases.
timeToWait milliseconds. The default priority is Thread.MIN_PRIORITY.
run - class to run
@Deprecated
public static RequestProcessor.Task postRequest(Runnable run,
int timeToWait)
RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases.
timeToWait milliseconds.
The default priority is Thread.MIN_PRIORITY.
run - class to runtimeToWait - to wait before execution
@Deprecated
public static RequestProcessor.Task postRequest(Runnable run,
int timeToWait,
int priority)
RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases.
timeToWait milliseconds. Given priority is assigned to the
request.
run - class to runtimeToWait - to wait before executionpriority - the priority from Thread.MIN_PRIORITY to Thread.MAX_PRIORITY
@Deprecated public static RequestProcessor.Task createRequest(Runnable run)
RequestProcessor
among different users and posting even blocking requests is inherently
deadlock-prone. See use cases.
run - action to run in the process
|
org.openide.util 8.1 | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||