The Thread Class

© 2012, Martin Rinehart

Prerequisite: Object-oriented programming in JavaScript.

The Thread class is explained in some detail in our Threads page. This page summarizes the API.

WARNING: These are not real threads. They do not permit the use of multiple cores on a multicore CPU. They will not divide long-running processes into smaller parts.

The Thread Constructor

Usage:

var my_thread = new MRlib323.Thread( runnable );

A "runnable" object is any object with a run() method.

Note: The constructor passes its parameters to an init() method that can be called by the constructor (or init() method) of a class that extends Thread.

The Thread Methods

The sleep() Method

Usage:

my_thread.sleep( millis );

The sleep() method pauses a running thread for millis milliseconds.

The start() Methods

The start() Method

Usage:

my_thread.start()

With no arguments, the start() method will call runnable.run() just once. This method is useful during debugging.

The start( millis ) Method

Usage:

my_thread.start( number_of_millis )

This is the standard start() method. It begins running the runnable.run() method, then pauses for number_of_millis and then repeats, until the Thread.stop() method is called.

The start( millis, number_of_runs ) Method

Usage:

my_thread.start( nillis, ntimes )

This version of start() repeats the runnable.run() method, with millis delays between them, for the specified number of repetitions and then stops.

The stop() Method

Usage:

my_thread.stop()

Stops executions of the runnable.run() method. (An indefinite pause, waiting on a subsequent start().)

my_thread.stop( num )

Stops execution after num more executions of runnable.run(). (An indefinite pause, waiting on a subsequent start().)

The toString() Method

Usage:

... '' + my_thread ...

Used during development.


Feedback: MartinRinehart at gmail dot com

# # #