Package ortus.boxlang.runtime.util
Class RequestThreadManager
java.lang.Object
ortus.boxlang.runtime.util.RequestThreadManager
I manage the threads for a request. Used by the bx:thread component and thread scopes
Every request has a thread manager
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringThe prefix for thread namesstatic final longThe default time to wait for a thread to stop when terminatingThe threads we are managing will be stored here alongside a structure of data: - context : ThreadBoxContext - startTicks : When the thread started - name : The thread name - metadata : A struct of metadata about the threadprotected IScopeThe thread scope for the request -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcompleteThread(Key name, String output, Throwable exception, Boolean interrupted) Marks a thread as completecreateThreadContext(IBoxContext context, Key name, IStruct attributes) Build a thread context for the given context and namestatic KeyensureThreadName(String name) Generates a thread name according to the given name.getThreadData(Key name) Gets the thread data for a thread.Get the thread group for the threads created by this managergetThreadMeta(Key name) Gets just the thread meta data for a thread.Key[]Gets the names of the threadsGets the thread scopebooleanCheck if a thread exists by namebooleanDetect if at least one threadvoidInterrupt ALL threads in this managervoidinterruptThread(Key name) Interrupt a thread by namebooleanVerify if the current thread is in a thread.booleanisThreadAlive(Key threadName) Verify if a thread is alivebooleanIsThreadInterrupted(IBoxContext context) Interrupt the current threadbooleanIsThreadInterrupted(Key threadName) Interrupt a named threadvoidjoinAllThreads(Integer timeout) Joins all threads in the request thread managervoidjoinThread(Key name, Integer timeout) Join a thread by namevoidjoinThreads(Array names, Integer timeout) Join an array of thread namesregisterThread(Key name, ThreadBoxContext context) Registers a thread with the managerstartThread(ThreadBoxContext context, Key name, String priority, Runnable task) Starts a non-virtual thread using the given context, name, priority, and task.startThread(ThreadBoxContext context, Key name, String priority, Runnable task, boolean virtual) Starts a thread using the given context, name, virtual, priority, task, and attributes of execution.voidterminateThread(Key name) This method is used to terminate a thread.
-
Field Details
-
DEFAULT_THREAD_PREFIX
The prefix for thread names- See Also:
-
DEFAULT_THREAD_WAIT_TIME
public static final long DEFAULT_THREAD_WAIT_TIMEThe default time to wait for a thread to stop when terminating- See Also:
-
threads
The threads we are managing will be stored here alongside a structure of data: - context : ThreadBoxContext - startTicks : When the thread started - name : The thread name - metadata : A struct of metadata about the thread -
threadScope
The thread scope for the request
-
-
Constructor Details
-
RequestThreadManager
public RequestThreadManager()
-
-
Method Details
-
registerThread
Registers a thread with the manager- Parameters:
name- The name of the threadcontext- The context of the thread- Returns:
- The thread metadata
-
getThreadMeta
Gets just the thread meta data for a thread. This is a subset of the metadata. Returns null if not found.- Parameters:
name- The name of the thread- Returns:
- The thread meta data or null if not found
-
getThreadData
Gets the thread data for a thread. Throws exception if not found.- Parameters:
name- The name of the thread- Returns:
- The thread data
-
completeThread
Marks a thread as complete- Parameters:
name- The name of the threadoutput- The output of the threadexception- The exception that caused the thread to completeinterrupted- Whether the thread was interrupted
-
ensureThreadName
Generates a thread name according to the given name. If the name is empty or null, a random name is generated.- Parameters:
name- The name of the thread- Returns:
- The generated thread name
-
createThreadContext
Build a thread context for the given context and name- Parameters:
context- The context initiating the threadname- The name of the thread- Returns:
- The thread context
-
startThread
Starts a non-virtual thread using the given context, name, priority, and task.- Parameters:
context- The thread context to run inname- The name of the thread, if empty or null, a random name is generatedpriority- The priority of the thread, can be "high", "low", or "normal", the default is "normal"task- The task to run in the thread, lambda or runnableattributes- The attributes to pass to the thread's local scope- Returns:
- The thread instance already started
-
startThread
public Thread startThread(ThreadBoxContext context, Key name, String priority, Runnable task, boolean virtual) Starts a thread using the given context, name, virtual, priority, task, and attributes of execution.- Parameters:
context- The thread context to run inname- The name of the thread, if empty or null, a random name is generatedpriority- The priority of the thread, can be "high", "low", or "normal", the default is "normal"task- The task to run in the thread, lambda or runnablevirtual- Whether the thread is virtual or notattributes- The attributes to pass to the thread's local scope- Returns:
- The thread instance already started
-
terminateThread
This method is used to terminate a thread. It's not foolproof and the JVM could still be running the thread after this method is called.We try to interrupt the thread first, then we wait for x milliseconds for the thread to stop. If it doesn't stop, we force kill it. Well at least we try to force it.
- Parameters:
name- The name of the thread- Throws:
BoxRuntimeException- If the thread is not found
-
IsThreadInterrupted
Interrupt the current thread- Parameters:
context- The context in which the BIF is being invoked.- Returns:
- true if the thread is interrupted
-
IsThreadInterrupted
Interrupt a named thread- Parameters:
threadName- The name of the thread- Returns:
- true if the thread is interrupted
-
isThreadAlive
Verify if a thread is alive- Parameters:
threadName- The name of the thread- Returns:
- true if the thread is alive
-
joinAllThreads
Joins all threads in the request thread manager- Parameters:
timeout- The timeout for the join
-
joinThreads
Join an array of thread names- Parameters:
timeout- The timeout for the joinname- The name of the thread
-
joinThread
Join a thread by nameThis method will join a thread by name. If the thread is not found, an exception is thrown. If the thread is found, it will be joined. If a timeout is provided, the join will be aborted if the timeout is reached.
- Parameters:
name- The name of the threadtimeout- The timeout for the join
-
interruptAllThreads
public void interruptAllThreads()Interrupt ALL threads in this manager -
interruptThread
Interrupt a thread by name- Parameters:
name- The name of the thread
-
hasThreads
public boolean hasThreads()Detect if at least one thread- Returns:
- true if there are threads
-
hasThread
Check if a thread exists by name- Parameters:
name- The name of the thread- Returns:
- true if the thread exists
-
getThreadScope
Gets the thread scope- Returns:
- The thread scope
-
getThreadGroup
Get the thread group for the threads created by this manager- Returns:
- The thread group
-
isInThread
public boolean isInThread()Verify if the current thread is in a thread.- Returns:
- true if the current thread is in a thread
-
getThreadNames
Gets the names of the threads- Returns:
- The names of the threads
-