public final class EasyThread

  1. Object
  2. EasyThread
An easy API for working with threads similar to call serially/and wait that allows us to create a thread and dispatch tasks to it.

Nested types

interface EasyThread.ErrorListenerCallback listener for errors on easy thread

Methods

public static EasyThread start(String name)Starts a new thread
public static void addGlobalErrorListener(EasyThread.ErrorListener err)Adds a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously.
public static void removeGlobalErrorListener(EasyThread.ErrorListener err)Removes a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously.
public <T> void run(RunnableWithResult<T> r, SuccessCallback<T> t)Runs the given object asynchronously on the thread and returns the result object
public void run(Runnable r)Runs the given runnable on the thread, the method returns immediately
public <T> T run(RunnableWithResultSync<T> r)Runs the given runnable on the thread and blocks until it completes, returns the value object
public void runAndWait(Runnable r)Invokes the given runnable on the thread and waits for its execution to complete
public void killWhenIdle()Stops the thread once everything already queued has run.
public boolean isFinished()Whether the thread has ended and will not run anything else.
public void awaitFinished()Blocks until the thread has left its loop.
public void kill()Stops the thread once the current task completes
public boolean isThisIt()Returns true if the current thread is the easy thread and false othewise similar to the isEDT method
public void addErrorListener(EasyThread.ErrorListener err)Adds a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously.
public void removeErrorListener(EasyThread.ErrorListener err)Removes a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously.
public void setPriority(int newPriority)Changes the priority of this EasyThread.

Inherited methods

Method details

start

public static EasyThread start(String name)
Starts a new thread

Parameters

name String
the display name for the thread

Returns

a new thread instance

addGlobalErrorListener

public static void addGlobalErrorListener(EasyThread.ErrorListener err)
Adds a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously. This method must never be invoked from within the resulting callback code!

Parameters

err EasyThread.ErrorListener
the error callback

removeGlobalErrorListener

public static void removeGlobalErrorListener(EasyThread.ErrorListener err)
Removes a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously. This method must never be invoked from within the resulting callback code!

Parameters

err EasyThread.ErrorListener
the error callback

run

public <T> void run(RunnableWithResult<T> r, SuccessCallback<T> t)
Runs the given object asynchronously on the thread and returns the result object

Parameters

r RunnableWithResult<T>
runs this method
t SuccessCallback<T>
object is passed to the success callback

run

public void run(Runnable r)
Runs the given runnable on the thread, the method returns immediately

Parameters

r Runnable
the runnable

run

public <T> T run(RunnableWithResultSync<T> r)
Runs the given runnable on the thread and blocks until it completes, returns the value object

Parameters

r RunnableWithResultSync<T>
the runnable with result that will execute on the thread

Returns

value returned by r

runAndWait

public void runAndWait(Runnable r)
Invokes the given runnable on the thread and waits for its execution to complete

Parameters

r Runnable
the runnable

killWhenIdle

public void killWhenIdle()

Stops the thread once everything already queued has run.

#kill() stops it after the current task, which drops whatever is behind that task – and a caller blocked on one of those never hears back. This one lets the queue drain first, so the thread ends without abandoning work that was handed to it. Anything offered after it has ended is refused rather than accepted and forgotten.

isFinished

public boolean isFinished()
Whether the thread has ended and will not run anything else.

Returns

true once the thread has left its loop

awaitFinished

public void awaitFinished()

Blocks until the thread has left its loop.

#isFinished() answers whether that has happened; this waits for it. The two are not the same question as “has a stop been asked for”: #kill() and #killWhenIdle() make this thread refuse new work immediately, while whatever it had already accepted keeps running. A caller that reads the refusal as “the thread is gone” and then touches what the thread was working on is racing the tasks still in flight.

Returns immediately when called from the thread itself, which cannot wait for its own exit.

kill

public void kill()
Stops the thread once the current task completes

isThisIt

public boolean isThisIt()
Returns true if the current thread is the easy thread and false othewise similar to the isEDT method

Returns

true if we are currently within this easy thread

addErrorListener

public void addErrorListener(EasyThread.ErrorListener err)
Adds a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously. This method must never be invoked from within the resulting callback code!

Parameters

err EasyThread.ErrorListener
the error callback

removeErrorListener

public void removeErrorListener(EasyThread.ErrorListener err)
Removes a callback for error events, notice that this code isn’t thread safe and should be invoked synchronously. This method must never be invoked from within the resulting callback code!

Parameters

err EasyThread.ErrorListener
the error callback

setPriority

public void setPriority(int newPriority)
Changes the priority of this EasyThread.

Parameters

newPriority int
priority to set this thread to