A program thread.
A thread is a light-weight execution unit within a process. A thread has its own stack and runs independently from other threads, only subject to thread scheduling controlled by the underlying managed platform or operating system.
Julian doesn't have a thread manager on its own. The threads are created and are scheduled by the platform (JVM) thread manager. However, except for the main thread, which is the default thread on which the script engine starts execution, all other threads are background threads and will terminate upon completion of the main thread.
Once started, threads cannot be stopped or suspended. However, they can be interrupted by calling interrupt(), yet such events will go unnoticed if the thread's business logic doesn't actively check and react to it. The only real effect an interruption can cause is to force the thread to exit sleeping.
Threads can synchronized with each other through a particular class: Lock. If threads are competing on certain resources, use a lock to guard the critical region.
Parent Class
Type | Name | Signature |
---|---|---|
constructor | Thread | public Thread(Function, string, System.Concurrency.ThreadPriority) |
method S | checkInterruption | public static bool checkInterruption() |
method S | create | public static Thread create(Function) |
method S | getCurrent | public static Thread getCurrent() |
method | getName | public String getName() |
method | getState | public ThreadState getState() |
method | interrupt | public void interrupt() |
method | join | public void join() |
method S | sleep | public static bool sleep(int) |
method | start | public void start() |
public Thread(Function fun, string name, ThreadPriority pri)
Create a thread with specified function, name and priority.
Parameters
public static bool checkInterruption()
Check if the thread has been interrupted. Also reset the interruption flag.
Returns
public static Thread create(Function fun)
A factory method to create a thread with a function. The thread will have a default name and normal priority.
Parameters
Returns
public static Thread getCurrent()
Get the Thread object for the currently running thread.
Returns
public String getName()
Get the name of this thread.
Returns
public ThreadState getState()
Get state of the thread.
Returns
public void interrupt()
Send interruption signal to this thread.
Note the receiving thread must have logic to check this and react accordingly. The sender must not assume such logic exists and therefore shall not solely rely on this to coordinate inter-thread works.
public void join()
Wait until the specified thread is finished.
This method will block until the thread finishes. Calling this on the current thread will throw IllegalStateException.
public static bool sleep(int periodInMillisec)
Let the current thread sleep for specified duration.
Parameters
Returns
public void start()
Start the thread.
Calling this on the current thread will throw IllegalStateException.