|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
ObjectInterruptable
public abstract class Interruptable
A base class that helps you define a piece of code
that might be invoked "too often", that is: a piece
of code that performs an expensive task that must
be restarted afresh whenever it is run at all.
This type of task often arises from user-initiated
GUI actions.
For example, you might design a complex, editable
Swing component that listens for changes in
an underlying data model. Changes to the model
could be triggered by user-initiated edit actions
on the Swing component, or by programmatic actions
initiated elsewhere. The period between rapidly
succeeding change events could be much shorter
than the time it takes to refresh the component.
Furhermore, you would prefer that the listening method
return immediately rather than blocking until it
finishes the refresh. In this case, we would
prefer to abandon the redrawing task whenever
a new change event occurs before we are finished.
This class solves exactly this problem.
All the setup you have to do is extend this class
and implement runImpl(Object)
. To use the class, instantiate
one instance of it and call start()
on that instance
whenever you want to run the expensive code.
Whever you call start()
, this class will interrupt previous
Threads spawned by calls to start()
.
However, it will be useful only if your implementation
of runImpl(Object)
calls Thread.sleep(n), n > 0,
every so often.
This prototypical example class
is a recommended use of Interruptable
,
and it illustrates the above example :
class ComplexEditableComponent extends JComponent { void eventDispatched( Event event ){ refresh( event.getHint() ); } void refresh( Hint hint ){myRunRefresh.start( hint )
; } class RunRefresh extendsInterruptable
{ public voidrunImpl( Object arg )
{ ComplexEditableComponent.this.refreshImpl( (Hint)arg ); } } void refreshImpl( Hint hint ){ try{ Thread.sleep(2); expensiveA(); Thread.sleep(2); expensiveB(); Thread.sleep(2); expensiveC(); }catch( InterruptedException e ){ cleanup(); } } RunRefresh myRunRefresh = new RunRefresh(); }
Nested Class Summary | |
---|---|
static interface |
Interruptable.Veto
Implement this interface to define a callback for deciding whether or not to go ahead with an interruption. |
Field Summary | |
---|---|
static boolean |
FLAG_DEBUG_OVERRIDE
|
static boolean |
FLAG_VERBOSE_OVERRIDE
|
static long |
LONG_PAUSE_GRACE_MILLIS
|
static String |
STR_NAME_PREFIX
|
static PrintStream |
STREAM_DEBUG
|
static PrintStream |
STREAM_VERBOSE
|
Constructor Summary | |
---|---|
Interruptable()
|
Method Summary | |
---|---|
void |
cancel()
|
boolean |
debug()
Override this to return true to print debug messages. |
ThreadGroup |
getCurrentParent()
|
String |
getName()
For identification purposes |
String |
getNameMethodOfInterest()
Override this to help print meaningful verbose messages. |
Object |
getSynch()
We use a single, devoted Object to synchronize calls to runImpl(Object) . |
void |
interrupt()
|
static int |
kill(ThreadGroup group)
|
static void |
printMethodStackTraceElement(Throwable throwable,
String methodname,
PrintStream stream,
boolean abbreviate)
|
void |
run()
Run the task in the current Thread, ie bypass the normal functioning of this class. |
Thread |
run(boolean threaded)
Run threaded or not. |
Thread |
run(boolean threaded,
Object arg1)
Run threaded or not. |
void |
run(Object arg1)
Run the task in the current Thread, ie bypass the normal functioning of this class. |
abstract void |
runImpl(Object arg1)
Implement this method to perform a expensive task. |
void |
setName(String name)
Set a descriptive name to identify this task |
void |
setParent(ThreadGroup group)
|
void |
setVeto(Interruptable.Veto veto)
Set a callback. |
Thread |
start()
Run the expensive task in a new Thread. |
Thread |
start(long millis)
Run the expensive task in a new Thread that waits for the specified number of milliseconds before beginning the task. |
Thread |
start(long millis,
Object arg1)
Run the expensive task in a new Thread that waits for the specified number of milliseconds before beginning the task. |
Thread |
start(Object arg1)
Run the expensive task in a new Thread. |
void |
stop()
|
boolean |
verbose()
Override this to return true to print debug messages. |
Methods inherited from class Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static boolean FLAG_DEBUG_OVERRIDE
public static boolean FLAG_VERBOSE_OVERRIDE
public static final long LONG_PAUSE_GRACE_MILLIS
public static final PrintStream STREAM_VERBOSE
public static final PrintStream STREAM_DEBUG
public static final String STR_NAME_PREFIX
Constructor Detail |
---|
public Interruptable()
Method Detail |
---|
public abstract void runImpl(Object arg1) throws InterruptedException
#checkInterrupted()
every so often to allow
the thread to be interrupted.
arg1
- The argument allows your task to take arguments
passed in by calls to start(Object)
InterruptedException
public boolean debug()
public boolean verbose()
public String getNameMethodOfInterest()
public final void run()
run
in interface Runnable
public void setVeto(Interruptable.Veto veto)
public void setName(String name)
public String getName()
public Object getSynch()
runImpl(Object)
. This method returns it.
runImpl(Object)
public void run(Object arg1)
arg1
- Passed on to runImpl(Object)
public Thread run(boolean threaded)
threaded
- If true, run the expensive task in
a new Thread (i.e. call start()
). If false,
run the task in the current Thread (i.e. call run()
).
public Thread run(boolean threaded, Object arg1)
threaded
- If true, run the expensive task in
a new Thread (i.e. call start(Object)
). If false,
run the task in the current Thread (i.e. call run(Object)
).arg1
- Passed on to runImpl(Object)
public Thread start()
public Thread start(Object arg1)
arg1
- Passed on to runImpl(Object)
public Thread start(long millis)
millis
- Milliseconds to sleep before calling runImpl(Object)
.public Thread start(long millis, Object arg1)
millis
- Milliseconds to sleep before calling runImpl(Object)
.arg1
- Passed on to runImpl(Object)
public void interrupt()
public void stop()
public void cancel()
public void setParent(ThreadGroup group)
public ThreadGroup getCurrentParent()
public static void printMethodStackTraceElement(Throwable throwable, String methodname, PrintStream stream, boolean abbreviate)
public static int kill(ThreadGroup group)
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |