Class ThreadingUtil
For background, see http://jmri.org/help/en/html/doc/Technical/Threads.shtml
Note this distinguishes "on layout", for example, Setting a sensor, from "on GUI", for example, manipulating the Swing GUI. That may not be an important distinction now, but it might be later, so we build it into the calls.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceInterface for use in ThreadingUtil's lambda interfacesstatic interfaceInterface for use in ThreadingUtil's lambda interfacesstatic interfaceInterface for use in ThreadingUtil's lambda interfaces -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanCheck whether a specific thread is running (or able to run) right now.static ThreadGroupGet the JMRI default thread group.static booleanCheck if on the GUI event dispatch thread.static booleanCheck if on the layout-operation thread.static booleanCheck whether a specific thread is currently waiting.static ThreadCreate a new thread in the JMRI groupstatic ThreadCreate a new thread in the JMRI group.static voidrequireGuiThread(org.slf4j.Logger logger) Check that a call is on the GUI thread.static voidrequireLayoutThread(org.slf4j.Logger logger) Check that a call is on the Layout thread.static voidRun some GUI-specific code before returningstatic TimerrunOnGUIDelayed(ThreadingUtil.ThreadAction ta, int delay) Run some GUI-specific code at some later point, at least a known time in the future.static voidRun some GUI-specific code at some later point.static voidRun some GUI-specific code before returning.static <E> ERun some GUI-specific code before returning a value.static voidRun some layout-specific code before returning.static TimerrunOnLayoutDelayed(ThreadingUtil.ThreadAction ta, int delay) Run some layout-specific code at some later point, at least a known time in the future.static voidRun some layout-specific code at some later point.static voidRun some layout-specific code before returning.static voidWarn if a thread is holding locks.
-
Constructor Details
-
ThreadingUtil
public ThreadingUtil()
-
-
Method Details
-
runOnLayout
Run some layout-specific code before returning.Typical uses:
ThreadingUtil.runOnLayout(() -> { sensor.setState(value); });- Parameters:
ta- What to run, usually as a lambda expression
-
runOnLayoutWithJmriException
public static void runOnLayoutWithJmriException(@Nonnull ThreadingUtil.ThreadActionWithJmriException ta) throws JmriException, RuntimeException Run some layout-specific code before returning. This method catches and rethrows JmriException and RuntimeException.Typical uses:
ThreadingUtil.runOnLayout(() -> { sensor.setState(value); });- Parameters:
ta- What to run, usually as a lambda expression- Throws:
JmriException- when an exception occursRuntimeException- when an exception occurs
-
runOnLayoutEventually
Run some layout-specific code at some later point.Please note the operation may have happened before this returns. Or later. No long-term guarantees.
Typical uses:
ThreadingUtil.runOnLayoutEventually(() -> { sensor.setState(value); });- Parameters:
ta- What to run, usually as a lambda expression
-
runOnLayoutDelayed
Run some layout-specific code at some later point, at least a known time in the future.There is no long-term guarantee about the accuracy of the interval.
Typical uses:
ThreadingUtil.runOnLayoutDelayed(() -> { sensor.setState(value); }, 1000);- Parameters:
ta- what to run, usually as a lambda expressiondelay- interval in milliseconds- Returns:
- reference to timer object handling delay so you can cancel if desired; note that operation may have already taken place.
-
isLayoutThread
Check if on the layout-operation thread.- Returns:
- true if on the layout-operation thread
-
runOnGUI
Run some GUI-specific code before returningTypical uses:
ThreadingUtil.runOnGUI(() -> { mine.setVisible(); });If an InterruptedException is encountered, it'll be deferred to the next blocking call via Thread.currentThread().interrupt()
- Parameters:
ta- What to run, usually as a lambda expression
-
runOnGUIWithJmriException
public static void runOnGUIWithJmriException(@Nonnull ThreadingUtil.ThreadActionWithJmriException ta) throws JmriException, RuntimeException Run some GUI-specific code before returning. This method catches and rethrows JmriException and RuntimeException.Typical uses:
ThreadingUtil.runOnGUI(() -> { mine.setVisible(); });If an InterruptedException is encountered, it'll be deferred to the next blocking call via Thread.currentThread().interrupt()
- Parameters:
ta- What to run, usually as a lambda expression- Throws:
JmriException- when an exception occursRuntimeException- when an exception occurs
-
runOnGUIwithReturn
Run some GUI-specific code before returning a value.Typical uses:
Boolean retval = ThreadingUtil.runOnGUIwithReturn(() -> { return mine.isVisible(); });If an InterruptedException is encountered, it'll be deferred to the next blocking call via Thread.currentThread().interrupt()
- Type Parameters:
E- generic- Parameters:
ta- What to run, usually as a lambda expression- Returns:
- the value returned by ta
-
runOnGUIEventually
Run some GUI-specific code at some later point.If invoked from the GUI thread, the work is guaranteed to happen only after the current routine has returned.
Typical uses:
ThreadingUtil.runOnGUIEventually( ()->{ mine.setVisible(); } );- Parameters:
ta- What to run, usually as a lambda expression
-
runOnGUIDelayed
Run some GUI-specific code at some later point, at least a known time in the future.There is no long-term guarantee about the accuracy of the interval.
Typical uses:
ThreadingUtil.runOnGUIDelayed( ()->{ mine.setVisible(); }, 1000);- Parameters:
ta- What to run, usually as a lambda expressiondelay- interval in milliseconds- Returns:
- reference to timer object handling delay so you can cancel if desired; note that operation may have already taken place.
-
isGUIThread
Check if on the GUI event dispatch thread.- Returns:
- true if on the event dispatch thread
-
newThread
Create a new thread in the JMRI group- Parameters:
runner- Runnable.- Returns:
- new Thread.
-
newThread
Create a new thread in the JMRI group.- Parameters:
runner- Thread runnable.name- Thread name.- Returns:
- New Thread.
-
getJmriThreadGroup
Get the JMRI default thread group. This should be passed to as the first argument to theThreadconstructor so we can track JMRI-created threads.- Returns:
- JMRI default thread group.
-
canThreadRun
Check whether a specific thread is running (or able to run) right now.- Parameters:
t- the thread to check- Returns:
- true is the specified thread is or could be running right now
-
isThreadWaiting
Check whether a specific thread is currently waiting.Note: This includes both waiting due to an explicit wait() call, and due to being blocked attempting to synchronize.
Note:
canThreadRun(Thread)andisThreadWaiting(Thread)should never simultaneously be true, but it might look that way due to sampling delays when checking on another thread.- Parameters:
t- the thread to check- Returns:
- true is the specified thread is or could be running right now
-
requireGuiThread
Check that a call is on the GUI thread. Warns (once) if not. Intended to be the run-time check mechanism for@InvokeOnGuiThreadIn this implementation, this is the same as
requireLayoutThread(org.slf4j.Logger)- Parameters:
logger- The logger object from the calling class, usually "log"
-
requireLayoutThread
Check that a call is on the Layout thread. Warns (once) if not. Intended to be the run-time check mechanism for@InvokeOnLayoutThreadIn this implementation, this is the same as
requireGuiThread(org.slf4j.Logger)- Parameters:
logger- The logger object from the calling class, usually "log"
-
warnLocks
Warn if a thread is holding locks. Used when transitioning to another context. -
getlastWarnLocksException
-