Singleton
caplin.core.scheduler

ThreadScheduler

Class used to improve the behaviour of applications on highly saturated machines.

When the browser becomes over saturated due to the rate of updates it receives, two problems can appear which are detrimental to the use of the browser as a real-time trading platform. These are:

  1. It's possible for UI events to get queued behind timer & interval events, decreasing the responsiveness of the application to user input.
  2. It's possible that the screen is not immediately refreshed when new updates are written to it — this can not happen until all pending JavaScript has been executed.

Constructor Summary

Attributes Name and Description
caplin.core.scheduler.ThreadScheduler( fSetTimeout, fClearTimeout)

Field Summary

Attributes Name and Description
UI_EVENT_YIELD_TIME

The time soon after which the thread scheduler will yield control so that any pending UI events can be processed.

Method Summary

Attributes Name and Description
void addThreadQueueListener(caplin.core.scheduler.ThreadQueueListener oListener)

Add a listener to be informed of updates to the current number of pending and due threads awaiting processing

void clearInterval(int nIntervalId)

A replacement for the native window.clearInterval() method.

void clearTimeout(int nTimeoutId)

A replacement for the native window.clearTimeout() method.

void preventTimerExecutionAfterThread(String sThreadId, int nTimeout)

Prevent execution of all timeouts and intervals, for the specified period of time, after a thread with the given id is executed.

void preventTimerExecutionFor(int nTimeout)

Prevent execution of all timeouts and intervals for the specified period of time.

int setInterval(Function fCallback, int nInterval, String sThreadId)

A replacement for the native window.setInterval() method.

void setThreadPriorities(Array pThreadIds)

Define the priorities with which threads will be scheduled.

int setTimeout(Function fCallback, int nTimeout, String sThreadId)

A replacement for the native window.setTimeout() method.

Constructor Detail

caplin.core.scheduler.ThreadScheduler( fSetTimeout, fClearTimeout)

Parameters
fSetTimeout
fClearTimeout

Field Detail

UI_EVENT_YIELD_TIME

The time soon after which the thread scheduler will yield control so that any pending UI events can be processed.

Method Detail

void addThreadQueueListener(caplin.core.scheduler.ThreadQueueListener oListener)

Add a listener to be informed of updates to the current number of pending and due threads awaiting processing

Parameters
caplin.core.scheduler.ThreadQueueListener oListener The object that will receive the call-back events.

void clearInterval(int nIntervalId)

A replacement for the native window.clearInterval() method.

This method replaces, and has the same API as window.clearInterval() — i.e. invoking window.clearInterval() actually causes this method to be invoked.

Parameters
int nIntervalId The id of the previously registered interval you wish to cancel.

void clearTimeout(int nTimeoutId)

A replacement for the native window.clearTimeout() method.

This method replaces, and has the same API as window.clearTimeout() — i.e. invoking window.clearTimeout() actually causes this method to be invoked.

Parameters
int nTimeoutId The id of the previously registered timeout you wish to cancel.

void preventTimerExecutionAfterThread(String sThreadId, int nTimeout)

Prevent execution of all timeouts and intervals, for the specified period of time, after a thread with the given id is executed.

The primary purpose of this method (and #preventTimerExecutionFor) are to ensure that JavaScript execution pauses for enough time to allow the browser to update the screen. Typically, this will only need to occur after the execution of any display updating threads, for example 'display throttle' and 'SL4B update batcher'.

By default, ThreadScheduler does not control the execution of UI events, and so is unable to ensure screen refreshes occur if any long running UI events are running at the same time. It is therefore the responsibility of application developers to ensure that any long running UI events defer their operation using window.setTimeout(), also bringing these threads under the control of ThreadScheduler.

Parameters
String sThreadId The name of the thread after which timer execution should be prevented.
int nTimeout The number of milliseconds timer execution will be prevented for.

void preventTimerExecutionFor(int nTimeout)

Prevent execution of all timeouts and intervals for the specified period of time.

Parameters
int nTimeout The number of milliseconds for which timer execution will be prevented.
See
preventTimerExecutionAfterThread

int setInterval(Function fCallback, int nInterval, String sThreadId)

A replacement for the native window.setInterval() method.

This method has the same API as window.setInterval(), except that it supports an optional sThreadId parameter that helps developers visualise thread usage, and is a pre-requisite for the use of the #preventTimerExecutionAfterThread method. You do not need to call caplin.core.scheduler.ThreadScheduler.setInterval() directly as it also replaces the native window.setInterval() method.

Parameters
Function fCallback The function that will be invoked each time the the interval elapses.
int nInterval The number of milliseconds between each invocation of the call-back.
String sThreadId A logical name for the thread that is being invoked (Optional).
Returns
A unique identifier that can be used to cancel this interval using #clearInterval.

void setThreadPriorities(Array pThreadIds)

Define the priorities with which threads will be scheduled.

The following example prioritises two named threads, while another thread is explicitly de-prioritised:

caplin.core.scheduler.ThreadScheduler.setThreadPriorities([
	 "highest-priority-thread",
	 "high-priority-thread",
	 "*",
	 "lowest-priority-thread"]);

Parameters
Array pThreadIds A list of thread identifiers, in priority order — a wildcard thread identifier (i.e. '*') can be used to explicitly de-prioritize any threads specified after that point.

int setTimeout(Function fCallback, int nTimeout, String sThreadId)

A replacement for the native window.setTimeout() method.

This method has the same API as window.setTimeout(), except that it supports an optional sThreadId parameter that helps developers visualise thread usage, and is a pre-requisite for the use of the #preventTimerExecutionAfterThread method. You do not need to call caplin.core.scheduler.ThreadScheduler.setTimeout() directly as it also replaces the native window.setTimeout() method.

Parameters
Function fCallback The function that will be invoked once the timeout period has elapsed.
int nTimeout The number of milliseconds until the call-back will be invoked.
String sThreadId A logical name for the thread that is being invoked (Optional).
Returns
A unique identifier that can be used to cancel this timer using #clearTimeout.