Myna. ThreadPool

Creates a ThreadPool that can execute a function multiple times in separate threads, with a maximum concurrency

Summary
Myna. ThreadPoolCreates a ThreadPool that can execute a function multiple times in separate threads, with a maximum concurrency
Functions
ThreadPoolConstructor for ThreadPool objects
runAdds a thread to the ThreadPool with the supplied arguments
getResultswaits until all threads are complete, and returns an array of their results in the order requested
Myna. ThreadPool. Futurewrapper for Java Future.
Functions
cancelattempts to cancel this thread
getwaits for a result for this thread and returns the result
isDonereturns true if the thread has completed
isCancelledreturns true if the thread has been cancelled

Functions

ThreadPool

Myna.ThreadPool = function (fn,
options)

Constructor for ThreadPool objects

Parameters

fnfunction to execute within the pool.  The params for each thread will be passed to run.  This function will be stripped of any closures or member properties.  This means this function should only reference parameters and standard JS and Myna globals
optionsOptional JS Object See Options below

Options

poolSizeMaximum number of concurrent threads.  Defaults to 1
backgroundIf true, threads will be run in the global background ThreadPool.  Otherwise a local ThreadPool is used and will be terminated at request end.  Default false.

run

Myna.ThreadPool.prototype.run = function()

Adds a thread to the ThreadPool with the supplied arguments

Parameters

anyparameters are passed to the thread function defined in the constructor

Returns Myna.ThreadPool.Future

getResults

Myna.ThreadPool.prototype.getResults = function (timeout)

waits until all threads are complete, and returns an array of their results in the order requested

Parameters

timeoutOptional, default wait until done time to wait in milliseconds (see <Date.getInterval>)

If a timeout occurs, all remaining results will be undefined

Myna. ThreadPool. Future

wrapper for Java Future.  Returned by Myna.ThreadPool.run

Parameters

futureJava Future instance
Summary
Functions
cancelattempts to cancel this thread
getwaits for a result for this thread and returns the result
isDonereturns true if the thread has completed
isCancelledreturns true if the thread has been cancelled

Functions

cancel

Myna.ThreadPool.Future.prototype.cancel = function (terminate)

attempts to cancel this thread

Parameters

terminateOptional, default false Attempts to terminate this thread if already running.

Note

Terminating threads will only work if the thread is listening to interrupt signals, e.g.  Thread.sleep().  You can check for this in Myna code by checking java.lang.Thread.interrupted() during loops

get

Myna.ThreadPool.Future.prototype.get = function (timeout)

waits for a result for this thread and returns the result

Parameters

timeoutOptional, default null (wait forever) If a positive integer, this should be the number of milliseconds to wait for a response from the thread. see Date.getInterval.  If no response within the timeout, an exception is thrown

Returns thread result or throws exception

Note

Even if you don’t set timeout, the request timeout $req.timeout still applies

isDone

Myna.ThreadPool.Future.prototype.isDone = function ()

returns true if the thread has completed

isCancelled

Myna.ThreadPool.Future.prototype.isCancelled = function ()

returns true if the thread has been cancelled

Myna.ThreadPool = function (fn,
options)
Constructor for ThreadPool objects
Myna.ThreadPool.prototype.run = function()
Adds a thread to the ThreadPool with the supplied arguments
Myna.ThreadPool.prototype.getResults = function (timeout)
waits until all threads are complete, and returns an array of their results in the order requested
Myna.ThreadPool.Future.prototype.cancel = function (terminate)
attempts to cancel this thread
Myna.ThreadPool.Future.prototype.get = function (timeout)
waits for a result for this thread and returns the result
Myna.ThreadPool.Future.prototype.isDone = function ()
returns true if the thread has completed
Myna.ThreadPool.Future.prototype.isCancelled = function ()
returns true if the thread has been cancelled
wrapper for Java Future.
Date.getInterval = function(interval,
count)
returns a time interval in milliseconds.
The maximum time in seconds this request should be allowed to run.