Thread CLASS

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

All Members


TypeNameSignature
constructorThreadpublic Thread(Function, string, System.Concurrency.ThreadPriority)
method ScheckInterruptionpublic static bool checkInterruption()
method Screatepublic static Thread create(Function)
method SgetCurrentpublic static Thread getCurrent()
methodgetNamepublic String getName()
methodgetStatepublic ThreadState getState()
methodinterruptpublic void interrupt()
methodjoinpublic void join()
method Ssleeppublic static bool sleep(int)
methodstartpublic void start()

Constructors


public Thread(Function fun, string name, ThreadPriority pri)

Create a thread with specified function, name and priority.

Parameters

  • fun The function to run on this thread.
  • name The name of this thread. If null, a default name will be assigned to it.
  • pri The priority of this thread.

Methods


public static bool checkInterruption()

Check if the thread has been interrupted. Also reset the interruption flag.

Returns

  • true if interrupted, with the interruption flag reset; false if not.

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

  • fun The function to run on this thread.

Returns

  • A thread ready to start.

public static Thread getCurrent()

Get the Thread object for the currently running thread.

Returns

  • The thread object representing the current thread (the one on which this method is called)

public String getName()

Get the name of this thread.

Returns

  • Thread's name.

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

  • periodInMillisec sleep duration in milliseconds

Returns

  • true if interrupted, with the interruption flag reset; false if time is up.

public void start()

Start the thread.

Calling this on the current thread will throw IllegalStateException.