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


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


my_thread.sleep( millis );

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

The start() Methods

The start() Method



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

The start( millis ) Method


my_thread.start( number_of_millis )

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

The start( millis, number_of_runs ) Method


my_thread.start( nillis, ntimes )

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

The stop() Method



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

my_thread.stop( num )

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

The toString() Method


... '' + my_thread ...

Used during development.

Feedback: MartinRinehart at gmail dot com

# # #