Hemi JavaScript Framework

Hemi.util.thread

Description

The Thread class provides a psuedo-thread implementation for Framework Objects. Each thread is sensitive to the object lifecycle, and can stop itself when the object is destroyed or when the framework is shutdown.

Class Index

static Hemi.util.thread.Thread

version

Creates a psuedo asynchronous thread.

See Example Thread #1 for an example of implementating the script on a page.

Index

Example Index

Method Index

Examples

Example Thread #1

Demonstrate how to use the Thread component.

Example Code

var MyThreadedObject = {
   handle_thread_run : function(oThread){
      /* basic demo */
      document.title = (new Date()).getTime();
   }
};
/* Prepare the MyThreadedObject for use with Engine */
Hemi.prepareObject("CustomThread","1.0",true,MyThreadedObject);
/* Create a new thread for MyThreadedObject*/
var oThread = Hemi.util.thread.newInstance(MyThreadedObject);
/* run the thread every 5 seconds */
oThread.run(5000);

Methods

newInstance

Creates a new instance of a psuedo thread. The thread is bound to the object lifetime of the specified object, so that when the object is explicitly destroyed or removed from the ObjectRegistry, the thread will also terminate.

Syntax

t = newInstance( o )

Parameters

Returns

t as Thread: A new ThreadInstance.

Hemi.util.thread.ThreadInstance

version

A psuedo thread that is created by invoking the static thread class newInstance method.

Index

Property Index

Method Index

Properties

object_config

Object API structure for storing sub structures: object_config.pointers and object_config.status.

Syntax

object = object.object_config

object_id

Unique instance identifier.

Syntax

String = object.object_id

object_type

The type of this object.

Syntax

String = object.object_type

object_version

Version of the object class.

Syntax

String = object.object_version

ready_state

Object load and execution state. Follows: 0 unitialized, 1 through 3 variant, 4 ready, 5 destroyed.

Syntax

int = object.ready_state

Methods

allStop

Invokes the stop method and prevents the thread from further execution.

Syntax

void allStop( )

destroy

Prepares the object for destruction.

Syntax

void destroy( )

getIsRunnable

Returns true if the thread can be run.

Syntax

b = getIsRunnable( )

Returns

b as boolean: Bit indicating whether the thread can be run.

getIsRunning

Returns true if the thread is running, false otherwise.

Syntax

b = getIsRunning( )

Returns

b as boolean: Bit indicating whether the thread is running.

getLastInterval

Returns the delay interval used to start the thread.

Syntax

i = getLastInterval( )

Returns

i as int: The interval in milliseconds used to start the thread.

getObjectId

Returns the unique id of the object.

Syntax

i = getObjectId( )

Returns

i as String: The unique object instance id.

getObjectType

Returns the type of the object.

Syntax

t = getObjectType( )

Returns

t as String: The type of the object instance.

getObjectVersion

Returns the version of the object.

Syntax

v = getObjectVersion( )

Returns

v as String: The version of the object instance.

getPointers

Returns the object_config.pointers sub structure.

Syntax

o = getPointers( )

Returns

o as object: The object_config.pointers substructure.

getReadyState

Returns the state of the object.

Syntax

s = getReadyState( )

Returns

s as int: The object ready state.

getStatus

Returns the object_config.status sub structure.

Syntax

o = getStatus( )

Returns

o as object: The object_config.status substructure.

handle_thread_run

Invoked when the thread is running an iteration. Method is virtual and can be overridden.

Syntax

void handle_thread_run( t )

Parameters

handle_thread_start

Invoked when the thread is starting. Method is virtual and can be overridden.

Syntax

void handle_thread_start( t )

Parameters

handle_thread_stop

Invoked when the thread is shutting down. Method is virtual and can be overridden.

Syntax

void handle_thread_stop( t )

Parameters

restart

Restarts the thread interval.

Syntax

b = restart( )

Returns

b as boolean: Bit indicating whether the thread was restarted.

run

Runs the thread with the specifed delay.

Syntax

b = run( d, b )

Parameters

Returns

b as boolean: Bit indicating whether the thread started running.

stop

Stops the thread.

Syntax

b = stop( b )

Parameters

Returns

b as boolean: Bit indicating whether the thread was stopped.