Class RequestThreadManager

java.lang.Object
ortus.boxlang.runtime.util.RequestThreadManager

public class RequestThreadManager extends Object
I manage the threads for a request. Used by the bx:thread component and thread scopes Every request has a thread manager
  • Field Details

    • DEFAULT_THREAD_PREFIX

      public static final String DEFAULT_THREAD_PREFIX
      The prefix for thread names
      See Also:
    • DEFAULT_THREAD_WAIT_TIME

      public static final long DEFAULT_THREAD_WAIT_TIME
      The default time to wait for a thread to stop when terminating
      See Also:
    • threads

      protected Map<Key,IStruct> 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

      protected IScope threadScope
      The thread scope for the request
  • Constructor Details

    • RequestThreadManager

      public RequestThreadManager()
  • Method Details

    • registerThread

      public IStruct registerThread(Key name, ThreadBoxContext context)
      Registers a thread with the manager
      Parameters:
      name - The name of the thread
      context - The context of the thread
      Returns:
      The thread metadata
    • getThreadMeta

      public IStruct getThreadMeta(Key name)
      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

      public IStruct getThreadData(Key name)
      Gets the thread data for a thread. Throws exception if not found.
      Parameters:
      name - The name of the thread
      Returns:
      The thread data
    • completeThread

      public void completeThread(Key name, String output, Throwable exception, Boolean interrupted)
      Marks a thread as complete
      Parameters:
      name - The name of the thread
      output - The output of the thread
      exception - The exception that caused the thread to complete
      interrupted - Whether the thread was interrupted
    • ensureThreadName

      public static Key ensureThreadName(String name)
      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

      public ThreadBoxContext createThreadContext(IBoxContext context, Key name, IStruct attributes)
      Build a thread context for the given context and name
      Parameters:
      context - The context initiating the thread
      name - The name of the thread
      Returns:
      The thread context
    • startThread

      public Thread startThread(ThreadBoxContext context, Key name, String priority, Runnable task)
      Starts a non-virtual thread using the given context, name, priority, and task.
      Parameters:
      context - The thread context to run in
      name - The name of the thread, if empty or null, a random name is generated
      priority - 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 runnable
      attributes - 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 in
      name - The name of the thread, if empty or null, a random name is generated
      priority - 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 runnable
      virtual - Whether the thread is virtual or not
      attributes - The attributes to pass to the thread's local scope
      Returns:
      The thread instance already started
    • terminateThread

      public void terminateThread(Key name)
      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

      public boolean IsThreadInterrupted(IBoxContext context)
      Interrupt the current thread
      Parameters:
      context - The context in which the BIF is being invoked.
      Returns:
      true if the thread is interrupted
    • IsThreadInterrupted

      public boolean IsThreadInterrupted(Key threadName)
      Interrupt a named thread
      Parameters:
      threadName - The name of the thread
      Returns:
      true if the thread is interrupted
    • isThreadAlive

      public boolean isThreadAlive(Key threadName)
      Verify if a thread is alive
      Parameters:
      threadName - The name of the thread
      Returns:
      true if the thread is alive
    • joinAllThreads

      public void joinAllThreads(Integer timeout)
      Joins all threads in the request thread manager
      Parameters:
      timeout - The timeout for the join
    • joinThreads

      public void joinThreads(Array names, Integer timeout)
      Join an array of thread names
      Parameters:
      timeout - The timeout for the join
      name - The name of the thread
    • joinThread

      public void joinThread(Key name, Integer timeout)
      Join a thread by name

      This 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 thread
      timeout - The timeout for the join
    • interruptAllThreads

      public void interruptAllThreads()
      Interrupt ALL threads in this manager
    • interruptThread

      public void interruptThread(Key name)
      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

      public boolean hasThread(Key name)
      Check if a thread exists by name
      Parameters:
      name - The name of the thread
      Returns:
      true if the thread exists
    • getThreadScope

      public IScope getThreadScope()
      Gets the thread scope
      Returns:
      The thread scope
    • getThreadGroup

      public ThreadGroup 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

      public Key[] getThreadNames()
      Gets the names of the threads
      Returns:
      The names of the threads