The Thread()
function is used to create concurrent threads.
The Thread()
function returns a Thread
object, which is used to manage created concurrent threads, thread communication, etc.
Thread(func, ...args)
Thread(...items)
The parameter ```func``` is a function for concurrent execution (passed by reference), and supports passing in anonymous functions. ```func``` can accept multiple parameters, which will be passed in through ```...args``` during concurrent execution. Therefore, the parameter list of ```func``` needs to be consistent with ```...args```.
func
true
function
The parameter ```arg``` is the actual parameter passed to ```func``` (i.e. the concurrent thread execution function) when the callback is executed; there may be multiple parameters ```arg```, and the parameter list of ```func``` needs to be consistent with ```...args```.
arg
false
string, number, bool, object, array, function, null value and other types supported by the system
The parameter ```item``` is an array containing the function references and their parameters to be executed concurrently. Multiple groups of ```item``` parameters can be passed in when calling the ```Thread``` function.
item
true
array
```javascript
function test1(a, b, c) {
Log("test1:", a, b, c)
}
function main() {
var t1 = threading.Thread(test1, 1, 2, 3)
var t2 = threading.Thread(function (msg) {
Log("msg:", msg)
}, "Hello thread2")
t1.join()
t2.join()
}
Create concurrent threads for both a custom function and an anonymous function.
function test1(msg) {
Log("msg:", msg)
test2("Hello test2")
}
function main() {
var t1 = threading.Thread(
[function(a, b, c) {Log(a, b, c)}, 1, 2, 3],
[test1, "Hello test1"],
[`function test2(msg) {Log("msg:", msg)}`])
t1.join()
}
Use the Thread(...items)
form to create concurrent threads and execute multiple functions sequentially.
function testFunc1(p) {
Log("testFunc1 p:", p)
}
function main() {
threading.Thread(function(pfn) {
var threadName = threading.currentThread().name()
var threadId = threading.currentThread().id()
pfn(`in thread threadName: ${threadName}, threadId: ${threadId}`)
}, testFunc1).join()
}
It supports passing parameters to concurrently executed functions.
function ml(input) {
const net = new brain.NeuralNetwork()
net.train([
{ input: [0, 0], output: [0] },
{ input: [0, 1], output: [1] },
{ input: [1, 0], output: [1] },
{ input: [1, 1], output: [0] },
])
return net.run(input)
}
function main() {
var ret = threading.Thread([ml, [1, 0]], [HttpQuery("https://unpkg.com/brain.js")]).join()
// ret: {"id":1,"terminated":false,"elapsed":337636000,"ret":{"0":0.9339330196380615}}
Log(ret)
}
It supports passing in function strings and can import external libraries dynamically for concurrent computing.
The thread function func
passed into the Thread()
function for concurrent execution runs in an isolated environment, so variables outside the thread cannot be directly referenced, and compilation will fail when referenced. At the same time, references to other closure functions are not supported within the thread. All APIs provided by the platform can be called within the thread, but other user-defined functions cannot be called.
When a thread is executed and is not referenced continuously, the underlying system will reclaim thread-related resources automatically, and there is no need to explicitly call the join()
function to release resources. If there is a continuous reference that prevents the release of resources, an error will be reported if the number of concurrent calls exceeds 2000: InternalError: too many routine wait, max is 2000
.
It supports backtesting system and live trading environment. All concurrent thread-related functions are only supported as code compatibility in the backtesting system and will not be actually executed by concurrent threads, so they will not be repeated in this chapter.
{@fun/Threads/threading/getThread getThread}, {@fun/Threads/threading/mainThread mainThread}, {@fun/Threads/threading/currentThread currentThread}, {@fun/Threads/threading/Lock Lock}, {@fun/Threads/threading/Condition Condition}, {@fun/Threads/threading/Event Event}, {@fun/Threads/threading/Dict Dict}, {@fun/Threads/threading/pending pending}, {@fun/Threads/threading/eventLoop eventLoop}
NetSettings getThread