Executes code in a separate thread
| Myna. Thread | Executes code in a separate thread |
| Functions | |
| Thread | Constructor function for Thread class |
| Properties | |
| javaThread | a local instance of java.lang.Thread |
| releaseOnJoin | should this thread’s memory be released when join or joinAll is called? |
| deleteOnJoin | should this thread’s metadata be deleted when join or joinAll is called? |
| captureOutput | should this thread capture generated content and return value from the subThread? |
| mynaThread | a reference to the running MynaThread instance. |
| Functions | |
| join | Pauses the current thread until this thread exits, and then returns thread function result |
| releaseSubThread | releases this Thread’s subThread so that its memory can be recovered |
| isRunning | returns true if this thread is still running |
| getContent | returns content generated by this thread |
| getReturnValue | returns the value returned by the thread function. |
| stop | stops this thread. |
| kill | kills this thread. |
| joinAll | Static function that calls join on all threads spawned from the current thread, and returns an array of their return values |
| getThreadArray | Static function that returns an array of all the threads spawned in the current thread |
| Myna. ThreadGroup | manages a collection of threads |
| Functions | |
| ThreadGroup | Constructor function for ThreadGroup class |
| getThreadArray | returns an array of all the threads spawned by this group |
| getThreadArray | join all the threads in this group. |
| spawn | calls ThreadGroup.add using this this group’s defaults and returns the generated thread. |
| getRunningThreads() | returns an array of the threads in this ThreadGroup that are still running |
| add | creates a thread, adds it to this group, and returns the generated thread |
Myna.Thread=function( f, args, priority )
Constructor function for Thread class
| f | function to execute |
| args | Optional, default [] array of arguments to pass to the function; |
| priority | Optional, default 0 This integer represents how often this thread should be scheduled as a percentage of normal priority. For example, priority 20 would be 20% more likely to run than normal requests during a given time slice. Priority -10 would be 10% LESS likely than normal requests to run during a given time slice |
Executes f in a separate thread. This can be used to take advantage of multiple processors be splitting a long running task into multiple functions running in parallel, or to execute non-interactive code in the background.
Each Thread runs in a separate Myna request with it’s own global ($) variables. Because the script path is copied from the parent thread, $application will be created the same as the parent thread and $application.onRequestStart() .onError() etc. will be fired at the appropriate times. This means that you should never call Myna.Thread from an application.sjs file, or your code will create an infinite thread spawning loop, and will error out after 5 levels.
You can pass variables from the current thread into Myna.Thread and the sub-thread will have access to them even after the parent thread exits
The parent thread can access the return value from f via Myna.Thread.getReturnValue or from Myna.Thread.join
The parent thread can access the generated output of f via Myna.Thread.getContent
Threads can themselves call Threads to a maximum depth of 5 levels. This prevents infinite Thread spawning.
if you do not need access to the subThread’s output, you can dramatically decrease memory usage by setting Thread.mynaThread to null
If you are running Myna.Thread in a loop, you can limit the number of concurrent Threads by making periodic calls to Myna.Thread.joinAll. See examples below
Reference to Thread instance
// ----- send an email in the background with a very low priority ---------
var t= new Myna.Thread(function(data){
new Myna.Mail({
to:data.toAddress,
from:data.fromAddress,
...
}).send()
},[$req.data],-90)
// ----- run all your queries at once --------------------------------------
new Myna.Thread(function(){
return new Myna.Query({
ds:"some ds",
sql:"..."
}).data
}])
new Myna.Thread(function(){
return new Myna.Query({
ds:"some other ds",
sql:"..."
}).data
})
// a destructuring assignment takes each row from the array returned from
// joinAll and assigns them to the variables in the assignment array
var [ qryData1, qryData2] = Myna.Thread.joinAll();
// ----- break up a memory intensive task into ----------------------------
// ----- a set number of concurrent threads ----------------------------
qry.data.forEach(function(row,index,array){
var t=new Myna.Thread(function(){
... bunch of work ...
},[])
//this frees each thread's memory when that thread is joined
t.releaseOnJoin=true;
//this stops after every three spawns or the end of the loop to
//wait for running threads to finish
if ((index+1)%3 == 0 || index == array.length-1) Myna.Thread.joinAll();
})
should this thread’s memory be released when join or joinAll is called? (Default: false)
Setting this to true will cause the subThread to be released when join or joinAll is called. If captureOutput is also true, then output is cached before releasing the the subThread
should this thread’s metadata be deleted when join or joinAll is called? (Default: false)
Setting this to true will cause the subThread’s metadata to be deleted when join or joinAll is called. This makes any return value or captured output unavailable.
This is a good choice for maximum memory efficiency
should this thread capture generated content and return value from the subThread? (Default: true)
Setting this to false will set mynaThread to null. Setting this value to true has no effect
a reference to the running MynaThread instance.
Setting this to null will prevent access to subThread output (see <Thread.getContent> and <Thread.getReturnValue>) but will also allow the subThread’s memory to be garbage collected immediately on completion
Myna.Thread.prototype.join=function( timeout, throwOnTimeout )
Pauses the current thread until this thread exits, and then returns thread function result
return value of thread function
| timeout | Optional, default 300000 (30 seconds) time in milliseconds to wait for the thread to finish. If the thread has not finished before the timeout control is returned to the current thread. A timeout of 0 disables the timeout |
| throwOnTimeout | Optional, default true If the thread has not finished before the timeout, throw an Error. |
Myna.Thread.prototype.releaseSubThread=function()
releases this Thread’s subThread so that its memory can be recovered
This function sets Thread.mynaThread to null. If Thread.captureOutput is true, this function will first cache the subThread’s return value and generated content. If you call this function a second time after setting Thread.captureOutput to false, then any cached values will be cleared
Myna.Thread.prototype.getContent=function()
returns content generated by this thread
Don’t expect to see anything while isRunning returns true
Myna.Thread.prototype.getReturnValue=function()
returns the value returned by the thread function.
Don’t expect to see anything while isRunning returns true
Myna.Thread.joinAll = function( timeout, throwOnTimeout, killOnTimeout )
Static function that calls join on all threads spawned from the current thread, and returns an array of their return values
| timeout | Optional, default 300000 (30 seconds) time in milliseconds to wait for all threads to finish. If all threads are not finished before the timeout control is returned to the current thread A timeout of 0 disables the timeout |
| throwOnTimeout | Optional, default true If all threads are not finished before the timeout, throw an Error. |
| killOnTimeout | Optional, default true If true, threads running past the timeout will be first stopped, then killed. Example: |
(10).times(function(i){
new Myna.Thread(function(i){
return i;
},[i])
})
Myna.printDump(Myna.Thread.joinAll())
Myna.Thread.getThreadArray = function()
Static function that returns an array of all the threads spawned in the current thread
manages a collection of threads
| Functions | |
| ThreadGroup | Constructor function for ThreadGroup class |
| getThreadArray | returns an array of all the threads spawned by this group |
| getThreadArray | join all the threads in this group. |
| spawn | calls ThreadGroup.add using this this group’s defaults and returns the generated thread. |
| getRunningThreads() | returns an array of the threads in this ThreadGroup that are still running |
| add | creates a thread, adds it to this group, and returns the generated thread |
Myna.ThreadGroup=function( options )
Constructor function for ThreadGroup class
| options | options object, see below |
| priority | Optional, default 0 default priority for each thread |
| args | Optional, default [] default array of arguments to pass to the threads created in this group |
| releaseOnJoin | Optional, default false default value for Thread.releaseOnJoin for each thread in this group |
| deleteOnJoin | Optional, default false default value for Thread.deleteOnJoin for each thread in this group |
| captureOutput | Optional, default true default value for Thread.captureOutput for each thread in this group |
| fn | Optional, default null default function to be used for each thread in this group |
| joinEvery | Optional, default 0 if a positive number, this thread group will join all running threads after this many spawns/adds. This allows you you put a limit on the number of threads running at a time. |
// ----- send several emails in the background with a very low priority ---------
var tg= new Myna.ThreadGroup({
priority:-90,
releaseOnJoin:true,
captureOutput:false,
fn:function(data){
new Myna.Mail({
to:data.toAddress,
from:data.fromAddress,
...
}).send()
}
})
qry.data.forEach(function(row){
tg.spawn(row);
})
// ----- run all your queries at once --------------------------------------
var queries= new Myna.ThreadGroup({
releaseOnJoin:true
})
qry.data.forEach(function(row){
tg.spawn(row);
})
tg.add(function(){
return new Myna.Query({
ds:"some ds",
sql:"..."
}).data
}])
tg.add(function(){
return new Myna.Query({
ds:"some other ds",
sql:"..."
}).data
})
// a destructuring assignment takes each row from the array returned from
// joinAll and assigns them to the variables in the assignment array
var [ qryData1, qryData2] = tg.join();
// ----- break up a memory intensive task into ----------------------------
// ----- a set number of concurrent threads ----------------------------
var workers= new Myna.ThreadGroup({
releaseOnJoin:true,
fn:function(row){
... bunch of work ...
},
joinEvery:3
})
qry.data.forEach(function(row,index,array){
workers.spawn(row);
})
var result = workers.join();
Myna.ThreadGroup.prototype.getThreadArray=function()
returns an array of all the threads spawned by this group
join all the threads in this group.
Takes the same parameters as Myna.Thread.joinAll
Myna.ThreadGroup.prototype.spawn=function()
calls ThreadGroup.add using this this group’s defaults and returns the generated thread.
<ThreadGroup.fn> must be defined. Any arguments passed to this function will be passed to the generated thread
Myna.ThreadGroup.prototype.getRunningThreads=function()
returns an array of the threads in this ThreadGroup that are still running
Myna.ThreadGroup.prototype.add=function( func, args, priority )
creates a thread, adds it to this group, and returns the generated thread
| func | Optional, default <ThreadGroup.fn> Function to execute in the the thread. If null or not defined <ThreadGroup.fn> will be used |
| args | Optional, default <ThreadGroup.args> Array of arguments to pass to the function. If null or not defined, <ThreadGroup.args> will be used |
| priority | Optional, default <ThreadGroup.priority> This integer represents how often this thread should be scheduled as a percentage of normal priority. For example, priority 20 would be 20% more likely to run than normal requests during a given time slice. Priority -10 would be 10% LESS likely than normal requests to run during a given time slice |
Constructor function for Thread class
Myna.Thread=function( f, args, priority )
Pauses the current thread until this thread exits, and then returns thread function result
Myna.Thread.prototype.join=function( timeout, throwOnTimeout )
Static function that calls join on all threads spawned from the current thread, and returns an array of their return values
Myna.Thread.joinAll = function( timeout, throwOnTimeout, killOnTimeout )
releases this Thread’s subThread so that its memory can be recovered
Myna.Thread.prototype.releaseSubThread=function()
returns true if this thread is still running
Myna.Thread.prototype.isRunning=function()
returns content generated by this thread
Myna.Thread.prototype.getContent=function()
returns the value returned by the thread function.
Myna.Thread.prototype.getReturnValue=function()
stops this thread.
Myna.Thread.prototype.stop=function()
kills this thread.
Myna.Thread.prototype.kill=function()
Static function that returns an array of all the threads spawned in the current thread
Myna.Thread.getThreadArray = function()
Constructor function for ThreadGroup class
Myna.ThreadGroup=function( options )
returns an array of all the threads spawned by this group
Myna.ThreadGroup.prototype.getThreadArray=function()
calls ThreadGroup.add using this this group’s defaults and returns the generated thread.
Myna.ThreadGroup.prototype.spawn=function()
creates a thread, adds it to this group, and returns the generated thread
Myna.ThreadGroup.prototype.add=function( func, args, priority )
returns an array of the threads in this ThreadGroup that are still running
Myna.ThreadGroup.prototype.getRunningThreads=function()