public class LooperThread extends Object implements Runnable
Looper.loop()
with optional iteration period. Looper thread has convenient methods to
start and stop thread execution and supports two iteration modes: continuous
and periodic
.
In continuous
mode looper thread executes loop iterations with processor speed. Looper instance is
usually waiting for some IO, takes care itself to sleep for some time or really has much computation to do. It is
looper instance responsibility to ensure processor is not abused.
To enable periodic
mode one should use a constructor that supplies a period value. Looper thread
executes internal sleep and invoke iteration with requested period. Implementation takes care to compensate for
iteration processing time but if it takes more that requested period looper thread works again in continuous mode.
In any case, looper implementation should consider that thread checks for running state between loops - for this reason blocking read operations are not allowed.
For your convenience here is a sample code.
class DemoLooper implements Looper, AsyncExceptionListener { private LooperThread thread; DemoLooper() { tread = new LooperThread(this); thread.start(); } void stop() { thread.stop(); } void loop() throws Exception { // execute iteration logic } void onAsyncException(Throwable throwable) { // handle loop iteration exception } }
Since looper is running in a separated thread is not possible to catch exceptions. If interested in handling loop
iteration exception one should make looper implementing AsyncExceptionListener
. It is also possible to set
exception listener after looper thread creation, even after thread start, using
setExceptionListener(AsyncExceptionListener)
.
Looper thread runs till explicit stop()
. This is true even if a loop execution throws exception; default
behavior is to dump stack and continue loop iterations. Anyway, if desirable to break the loop on exception one can
use setBreakOnException(boolean)
to set breakOnException
flag to true.
Modifier and Type | Field and Description |
---|---|
private AtomicBoolean |
breakOnException
break thread looper on exception, default to false.
|
private static int |
EXCEPTION_TIMEOUT
If looper thread works in
continuous mode add a timeout on exception loop in order to avoid hogging
the thread. |
private AsyncExceptionListener |
exceptionListener
Asynchronous exception listener.
|
private long |
exceptionTimestamp
Keep track of the previous exception in order to prevent exceptions to occur to often.
|
private static Log |
log
Class logger.
|
private Looper |
looper
Looper instance to invoke repetitively.
|
private int |
loopPeriod
Optional loop iteration period in milliseconds.
|
private AtomicBoolean |
running
Running state.
|
private static int |
SAMPLING_PERIOD
Looper tests
running flag with this sampling period, expressed in milliseconds. |
private static int |
STARTUP_TIMEOUT
Looper thread start-up timeout.
|
private static int |
STOP_TIMEOUT
Looper thread stop timeout.
|
private Thread |
thread
Underlying Java Thread.
|
Constructor and Description |
---|
LooperThread(Looper looper)
Create looper thread.
|
LooperThread(Looper looper,
int period)
Create looper thread with iteration period.
|
Modifier and Type | Method and Description |
---|---|
boolean |
isRunning()
Return true if this looper thread is running.
|
void |
join(long millis)
Waits at most millis milliseconds for this thread to die.
|
void |
run() |
void |
setBreakOnException(boolean breakOnException)
Set flag for breaking looper on exception.
|
void |
setExceptionListener(AsyncExceptionListener exceptionListener)
Set asynchronous exception listener to handle loop exceptions.
|
private static void |
sleep(long period)
Interruptible thread sleep.
|
void |
start()
Start the thread and wait for its actual startup.
|
void |
stop()
Stop thread and waits for its graceful close.
|
private static void |
wait(Object lock,
int timeout)
Wait notification on synchronized object for specified period of time.
|
private static final Log log
private static final int STARTUP_TIMEOUT
private static final int STOP_TIMEOUT
stop()
method returns without ensuring thread graceful
close. Note that Looper.loop()
processing duration should not exceed this timeout.private static final int EXCEPTION_TIMEOUT
continuous
mode add a timeout on exception loop in order to avoid hogging
the thread.private static final int SAMPLING_PERIOD
running
flag with this sampling period, expressed in milliseconds. Anyway, if loop period is
smaller uses it instead.private final Thread thread
private final Looper looper
private final int loopPeriod
private final AtomicBoolean running
private final AtomicBoolean breakOnException
private volatile AsyncExceptionListener exceptionListener
private long exceptionTimestamp
public LooperThread(Looper looper)
AsyncExceptionListener
it is also registered as
exception listener.looper
- looper instance.public LooperThread(Looper looper, int period)
period
is expressed in milliseconds and should
be positive; if zero looper thread is configured in continuous mode. If looper instance implements
AsyncExceptionListener
it is also registered as exception listener.looper
- looper instance,period
- loop iteration period, in milliseconds, possible zero.IllegalArgumentException
- if looper instance is null or period is not positive or zero.public void setBreakOnException(boolean breakOnException)
breakOnException
- break on exception flag.public void setExceptionListener(AsyncExceptionListener exceptionListener)
exceptionListener
- asynchronous exception listener, possible null.exceptionListener
public void start()
public void stop()
STOP_TIMEOUT
exceeds this method does
not guarantee graceful thread close.
If thread is not alive, see Thread.isAlive()
, this method does nothing.
public boolean isRunning()
public void join(long millis) throws InterruptedException
millis
- the time to wait in milliseconds.InterruptedException
- if any thread has interrupted the current thread. The interrupted status of the
current thread is cleared when this exception is thrown.private static void wait(Object lock, int timeout)
lock
- synchronized object to wait on, possible zero,timeout
- timeout in milliseconds.private static void sleep(long period)
period
- thread sleep period.Copyright © 2018. All rights reserved.