Class ConcurrencyBuiltinFunctions

java.lang.Object
org.daiitech.naftah.builtin.functions.ConcurrencyBuiltinFunctions

public final class ConcurrencyBuiltinFunctions extends Object
Concurrency-related built-in functions for manipulating threads, accessing thread information, controlling execution, and managing thread states.

This class offers a set of static methods to work with:

  • Threads (current, named, interrupted, priority, etc.)
  • Tasks (await, cancel, stop, join, etc.)
  • Channels (send, receive, name)
  • Actors (send message, stop, join, status, etc.)

This class is final and cannot be instantiated. Attempting to create an instance will throw a NaftahBugError.

Author:
Chakib Daii
  • Constructor Details

    • ConcurrencyBuiltinFunctions

      private ConcurrencyBuiltinFunctions()
      Private constructor to prevent instantiation. Throws NaftahBugError if called.
  • Method Details

    • currentThread

      public static NaftahObject currentThread()
      Returns the currently executing thread.
      Returns:
      the current Thread
    • getThreadName

      public static String getThreadName(NaftahObject threadObject)
      Returns the name of the specified thread.
      Parameters:
      threadObject - the thread whose name to return
      Returns:
      the name of the thread
    • getCurrentThreadName

      public static String getCurrentThreadName()
      Returns the name of the currently executing thread.
      Returns:
      the current thread's name
    • setCurrentThreadName

      public static void setCurrentThreadName(String newName)
      Sets the name of the currently executing thread.
      Parameters:
      newName - the new name for the current thread
    • setThreadName

      public static void setThreadName(NaftahObject threadObject, String newName)
      Sets the name of the specified thread.
      Parameters:
      threadObject - the thread to rename
      newName - the new name for the thread
    • sleep

      public static void sleep(Number millis)
      Causes the currently executing thread to sleep for the specified number of milliseconds.
      Parameters:
      millis - the length of time to sleep in milliseconds
      Throws:
      IllegalStateException - if the sleep is interrupted
    • sleep

      public static void sleep(Number millis, Number nanos)
      Causes the currently executing thread to sleep for the specified number of milliseconds plus an additional nanoseconds adjustment.
      Parameters:
      millis - the length of time to sleep in milliseconds
      nanos - additional nanoseconds to sleep (0–999999)
      Throws:
      IllegalStateException - if the sleep is interrupted
    • sleep

      public static void sleep(NaftahDuration duration)
      Causes the currently executing thread to sleep for the specified duration.
      Parameters:
      duration - the duration to sleep
      Throws:
      IllegalStateException - if the sleep is interrupted
    • yieldThread

      public static void yieldThread()
      Causes the currently executing thread to yield execution to other threads.
    • isInterrupted

      public static boolean isInterrupted(NaftahObject threadObject)
      Checks whether the specified thread has been interrupted.
      Parameters:
      threadObject - the thread to check
      Returns:
      true if the thread is interrupted, false otherwise
    • isCurrentThreadInterrupted

      public static boolean isCurrentThreadInterrupted()
      Checks whether the currently executing thread has been interrupted.
      Returns:
      true if the current thread is interrupted, false otherwise
    • interruptThread

      public static void interruptThread(NaftahObject threadObject)
      Interrupts the specified thread.
      Parameters:
      threadObject - the thread to interrupt
    • interruptCurrentThread

      public static void interruptCurrentThread()
      Interrupts the currently executing thread.
    • getThreadPriority

      public static int getThreadPriority(NaftahObject threadObject)
      Returns the priority of the specified thread.
      Parameters:
      threadObject - the thread to query
      Returns:
      the priority of the thread
    • getCurrentThreadPriority

      public static int getCurrentThreadPriority()
      Returns the priority of the currently executing thread.
      Returns:
      the current thread's priority
    • setCurrentThreadPriority

      public static void setCurrentThreadPriority(Number priority)
      Sets the priority of the currently executing thread.
      Parameters:
      priority - the new priority (1–10)
    • setThreadPriority

      public static void setThreadPriority(NaftahObject threadObject, Number priority)
      Sets the priority of the specified thread.
      Parameters:
      threadObject - the thread to modify
      priority - the new priority (1–10)
    • getThreadId

      public static long getThreadId(NaftahObject threadObject)
      Returns the unique ID of the specified thread.
      Parameters:
      threadObject - the thread to query
      Returns:
      the thread's unique ID
    • getCurrentThreadId

      public static long getCurrentThreadId()
      Returns the unique ID of the currently executing thread.
      Returns:
      the current thread's ID
    • await

      public static Object await(Task<?> task)
      Waits for the specified task to complete and returns its result.
      Parameters:
      task - the task to wait for
      Returns:
      the result of the task
      Throws:
      NaftahBugError - if the task throws an exception
    • isDone

      public static boolean isDone(Task<?> task)
      Checks whether the specified task is completed.
      Parameters:
      task - the task to query
      Returns:
      true if the task is done, false otherwise
    • isCancelled

      public static boolean isCancelled(Task<?> task)
      Checks whether the specified task has been cancelled.
      Parameters:
      task - the task to query
      Returns:
      true if the task was cancelled, false otherwise
    • cancel

      public static boolean cancel(Task<?> task, boolean interrupt)
      Cancels the specified task.
      Parameters:
      task - the task to cancel
      interrupt - true to interrupt if running, false otherwise
      Returns:
      true if the task was cancelled, false otherwise
    • getTaskId

      public static long getTaskId(Task<?> task)
      Returns the unique ID of the specified task.
      Parameters:
      task - the task to query
      Returns:
      the task's unique ID
    • stopTask

      public static void stopTask(Task<?> task)
      Stops the specified task by interrupting its executing thread.
      Parameters:
      task - the task to stop
    • joinTask

      public static void joinTask(Task<?> task)
      Waits for the specified task's thread to finish execution.
      Parameters:
      task - the task to join
      Throws:
      NaftahBugError - if the current thread is interrupted while waiting
    • isTaskAlive

      public static boolean isTaskAlive(Task<?> task)
      Checks whether the specified task's thread is still alive.
      Parameters:
      task - the task to query
      Returns:
      true if the task thread is alive, false otherwise
    • getTaskThread

      public static NaftahObject getTaskThread(Task<?> task)
      Returns the thread executing the specified task.
      Parameters:
      task - the task to query
      Returns:
      the task's executing thread
    • createChannel

      public static Channel<Object> createChannel(String name)
      Creates a new channel with the specified name.
      Parameters:
      name - the name of the channel
      Returns:
      a new Channel instance
    • send

      public static void send(Channel<Object> channel, Object value)
      Sends a value to the specified channel. This operation blocks if the channel is full.
      Parameters:
      channel - the channel to send to
      value - the value to send
      Throws:
      NaftahBugError - if the thread is interrupted while sending
    • receive

      public static Object receive(Channel<Object> channel)
      Receives a value from the specified channel. This operation blocks if the channel is empty.
      Parameters:
      channel - the channel to receive from
      Returns:
      the received value
      Throws:
      NaftahBugError - if the thread is interrupted while receiving
    • channelName

      public static String channelName(Channel<?> channel)
      Returns the name of the specified channel.
      Parameters:
      channel - the channel to query
      Returns:
      the channel's name
    • sendToActor

      public static boolean sendToActor(Actor<Object> actor, Object msg)
      Sends a message to the specified actor asynchronously.
      Parameters:
      actor - the actor to send to
      msg - the message to send
      Returns:
      true if the message was accepted, false otherwise
    • stopActor

      public static void stopActor(Actor<?> actor)
      Stops the specified actor gracefully.
      Parameters:
      actor - the actor to stop
    • joinActor

      public static void joinActor(Actor<?> actor)
      Waits for the actor's thread to finish execution.
      Parameters:
      actor - the actor to join
      Throws:
      NaftahBugError - if the current thread is interrupted while waiting
    • isActorAlive

      public static boolean isActorAlive(Actor<?> actor)
      Checks whether the actor's thread is alive.
      Parameters:
      actor - the actor to query
      Returns:
      true if the actor thread is alive, false otherwise
    • isActorRunning

      public static boolean isActorRunning(Actor<?> actor)
      Checks whether the actor is currently running.
      Parameters:
      actor - the actor to query
      Returns:
      true if the actor is running, false otherwise
    • getActorName

      public static String getActorName(Actor<?> actor)
      Returns the name of the actor.
      Parameters:
      actor - the actor to query
      Returns:
      the actor's name
    • getActorThread

      public static NaftahObject getActorThread(Actor<?> actor)
      Returns the thread executing the actor.
      Parameters:
      actor - the actor to query
      Returns:
      the actor's thread