Class ConcurrencyBuiltinFunctions
java.lang.Object
org.daiitech.naftah.builtin.functions.ConcurrencyBuiltinFunctions
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 Summary
ConstructorsModifierConstructorDescriptionprivatePrivate constructor to prevent instantiation. -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectWaits for the specified task to complete and returns its result.static booleanCancels the specified task.static StringchannelName(Channel<?> channel) Returns the name of the specified channel.createChannel(String name) Creates a new channel with the specified name.static NaftahObjectReturns the currently executing thread.static StringgetActorName(Actor<?> actor) Returns the name of the actor.static NaftahObjectgetActorThread(Actor<?> actor) Returns the thread executing the actor.static longReturns the unique ID of the currently executing thread.static StringReturns the name of the currently executing thread.static intReturns the priority of the currently executing thread.static longReturns the unique ID of the specified task.static NaftahObjectgetTaskThread(Task<?> task) Returns the thread executing the specified task.static longgetThreadId(NaftahObject threadObject) Returns the unique ID of the specified thread.static StringgetThreadName(NaftahObject threadObject) Returns the name of the specified thread.static intgetThreadPriority(NaftahObject threadObject) Returns the priority of the specified thread.static voidInterrupts the currently executing thread.static voidinterruptThread(NaftahObject threadObject) Interrupts the specified thread.static booleanisActorAlive(Actor<?> actor) Checks whether the actor's thread is alive.static booleanisActorRunning(Actor<?> actor) Checks whether the actor is currently running.static booleanisCancelled(Task<?> task) Checks whether the specified task has been cancelled.static booleanChecks whether the currently executing thread has been interrupted.static booleanChecks whether the specified task is completed.static booleanisInterrupted(NaftahObject threadObject) Checks whether the specified thread has been interrupted.static booleanisTaskAlive(Task<?> task) Checks whether the specified task's thread is still alive.static voidWaits for the actor's thread to finish execution.static voidWaits for the specified task's thread to finish execution.static ObjectReceives a value from the specified channel.static voidSends a value to the specified channel.static booleansendToActor(Actor<Object> actor, Object msg) Sends a message to the specified actor asynchronously.static voidsetCurrentThreadName(String newName) Sets the name of the currently executing thread.static voidsetCurrentThreadPriority(Number priority) Sets the priority of the currently executing thread.static voidsetThreadName(NaftahObject threadObject, String newName) Sets the name of the specified thread.static voidsetThreadPriority(NaftahObject threadObject, Number priority) Sets the priority of the specified thread.static voidCauses the currently executing thread to sleep for the specified number of milliseconds.static voidCauses the currently executing thread to sleep for the specified number of milliseconds plus an additional nanoseconds adjustment.static voidsleep(NaftahDuration duration) Causes the currently executing thread to sleep for the specified duration.static voidStops the specified actor gracefully.static voidStops the specified task by interrupting its executing thread.static voidCauses the currently executing thread to yield execution to other threads.
-
Constructor Details
-
ConcurrencyBuiltinFunctions
private ConcurrencyBuiltinFunctions()Private constructor to prevent instantiation. ThrowsNaftahBugErrorif called.
-
-
Method Details
-
currentThread
Returns the currently executing thread.- Returns:
- the current
Thread
-
getThreadName
Returns the name of the specified thread.- Parameters:
threadObject- the thread whose name to return- Returns:
- the name of the thread
-
getCurrentThreadName
Returns the name of the currently executing thread.- Returns:
- the current thread's name
-
setCurrentThreadName
Sets the name of the currently executing thread.- Parameters:
newName- the new name for the current thread
-
setThreadName
Sets the name of the specified thread.- Parameters:
threadObject- the thread to renamenewName- the new name for the thread
-
sleep
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
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 millisecondsnanos- additional nanoseconds to sleep (0–999999)- Throws:
IllegalStateException- if the sleep is interrupted
-
sleep
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
Checks whether the specified thread has been interrupted.- Parameters:
threadObject- the thread to check- Returns:
trueif the thread is interrupted,falseotherwise
-
isCurrentThreadInterrupted
public static boolean isCurrentThreadInterrupted()Checks whether the currently executing thread has been interrupted.- Returns:
trueif the current thread is interrupted,falseotherwise
-
interruptThread
Interrupts the specified thread.- Parameters:
threadObject- the thread to interrupt
-
interruptCurrentThread
public static void interruptCurrentThread()Interrupts the currently executing thread. -
getThreadPriority
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
Sets the priority of the currently executing thread.- Parameters:
priority- the new priority (1–10)
-
setThreadPriority
Sets the priority of the specified thread.- Parameters:
threadObject- the thread to modifypriority- the new priority (1–10)
-
getThreadId
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
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
Checks whether the specified task is completed.- Parameters:
task- the task to query- Returns:
trueif the task is done,falseotherwise
-
isCancelled
Checks whether the specified task has been cancelled.- Parameters:
task- the task to query- Returns:
trueif the task was cancelled,falseotherwise
-
cancel
Cancels the specified task.- Parameters:
task- the task to cancelinterrupt-trueto interrupt if running,falseotherwise- Returns:
trueif the task was cancelled,falseotherwise
-
getTaskId
Returns the unique ID of the specified task.- Parameters:
task- the task to query- Returns:
- the task's unique ID
-
stopTask
Stops the specified task by interrupting its executing thread.- Parameters:
task- the task to stop
-
joinTask
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
Checks whether the specified task's thread is still alive.- Parameters:
task- the task to query- Returns:
trueif the task thread is alive,falseotherwise
-
getTaskThread
Returns the thread executing the specified task.- Parameters:
task- the task to query- Returns:
- the task's executing thread
-
createChannel
Creates a new channel with the specified name.- Parameters:
name- the name of the channel- Returns:
- a new
Channelinstance
-
send
Sends a value to the specified channel. This operation blocks if the channel is full.- Parameters:
channel- the channel to send tovalue- the value to send- Throws:
NaftahBugError- if the thread is interrupted while sending
-
receive
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
Returns the name of the specified channel.- Parameters:
channel- the channel to query- Returns:
- the channel's name
-
sendToActor
Sends a message to the specified actor asynchronously.- Parameters:
actor- the actor to send tomsg- the message to send- Returns:
trueif the message was accepted,falseotherwise
-
stopActor
Stops the specified actor gracefully.- Parameters:
actor- the actor to stop
-
joinActor
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
Checks whether the actor's thread is alive.- Parameters:
actor- the actor to query- Returns:
trueif the actor thread is alive,falseotherwise
-
isActorRunning
Checks whether the actor is currently running.- Parameters:
actor- the actor to query- Returns:
trueif the actor is running,falseotherwise
-
getActorName
Returns the name of the actor.- Parameters:
actor- the actor to query- Returns:
- the actor's name
-
getActorThread
Returns the thread executing the actor.- Parameters:
actor- the actor to query- Returns:
- the actor's thread
-