Class Looper
- java.lang.Object
-
- io.github.pr0methean.betterrandom.util.Looper
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
SimpleRandomSeeder
public abstract class Looper extends Object implements Serializable
Wraps a thread that loops a given task until interrupted, with the iterations being transactional.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected static ThreadFactory
DEFAULT_THREAD_FACTORY
Singleton-ization ofExecutors.defaultThreadFactory()
.protected ThreadFactory
factory
TheThreadFactory
used to create (and, if necessary, replace) the thread.protected Lock
lock
The thread holds this lock whenever it is runningiterate()
.protected Thread
thread
The thread where this looper's loop is running.protected Lock
threadLock
The looper holds this lock whenever it is reading or writing the state of the underlying thread (including replacing that thread).
-
Constructor Summary
Constructors Modifier Constructor Description protected
Looper()
Constructs a Looper with all properties as defaults.protected
Looper(String name)
Constructs a Looper with a thread name.protected
Looper(ThreadFactory factory)
Constructs a Looper with a given thread factory.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
interrupt()
Interrupts the thread if it's running.boolean
isRunning()
Returns whether there is a running thread executing thisLooper
's loop.protected abstract boolean
iterate()
The task that will be iterated until it returns false.protected void
start()
Starts the thread if it's not already running, creating it if it doesn't exist, has died or has beeninterrupt()
ed.
-
-
-
Field Detail
-
DEFAULT_THREAD_FACTORY
protected static final ThreadFactory DEFAULT_THREAD_FACTORY
Singleton-ization ofExecutors.defaultThreadFactory()
.
-
threadLock
protected final Lock threadLock
The looper holds this lock whenever it is reading or writing the state of the underlying thread (including replacing that thread).
-
thread
protected transient volatile Thread thread
The thread where this looper's loop is running.
-
factory
protected final ThreadFactory factory
TheThreadFactory
used to create (and, if necessary, replace) the thread.
-
-
Constructor Detail
-
Looper
protected Looper()
Constructs a Looper with all properties as defaults. The thread is not started in the constructor, because subclass fields won't have been initialized.
-
Looper
protected Looper(String name)
Constructs a Looper with a thread name. The thread is not started in the constructor, because subclass fields won't have been initialized.- Parameters:
name
- the name of the thread to create
-
Looper
protected Looper(ThreadFactory factory)
Constructs a Looper with a given thread factory. The thread is not started in the constructor, because subclass fields won't have been initialized.- Parameters:
factory
- the thread factory that will create this instance's thread
-
-
Method Detail
-
isRunning
public boolean isRunning()
Returns whether there is a running thread executing thisLooper
's loop.- Returns:
- true if this has a running thread; false otherwise
-
iterate
protected abstract boolean iterate() throws InterruptedException
The task that will be iterated until it returns false. Cannot be abstract for serialization reasons, but must be overridden in subclasses if they are instantiated without a targetRunnable
.- Returns:
- true if this thread should iterate again.
- Throws:
InterruptedException
- if interrupted in mid-execution.
-
start
protected void start()
Starts the thread if it's not already running, creating it if it doesn't exist, has died or has beeninterrupt()
ed.
-
interrupt
public void interrupt()
Interrupts the thread if it's running. The thread will be replaced by a new one the next timestart()
is called.
-
-