threading.Thread
Creates concurrent threads.
Thread(func, ...args)
Thread(...items)Examples
-
Create a concurrent thread with both a custom function and an anonymous function simultaneously.
javascriptfunction 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() } -
Use the
Thread(...items)form to run several groups of functions in one thread, the last group first (a source string defining test2), then the second group (test1 calls test2), and the first group last.javascriptfunction 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() } -
Support passing functions as parameters to concurrently executing functions.
javascriptfunction 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() } -
Support passing function strings to dynamically import external libraries for concurrent computation.
javascriptfunction 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,"error":null,"ret":{"0":0.9339330196380615}} Log(ret) }
Returns
| Type | Description |
|
|
Arguments
| Name | Type | Required | Description |
func | function | Yes | The parameter |
arg | string / number / bool / object / array / function / any | No | The parameter |
item | array | Yes | The parameter |
See Also
Remarks
The thread function func passed to the Thread() function for concurrent execution runs in an isolated environment, so it cannot directly reference variables outside the thread; doing so makes the Thread() call throw external variables reference are not allowed. Additionally, referencing other closure functions is not supported within the thread. All APIs provided by the platform can be called inside the thread, but user-defined functions cannot be called directly; pass the functions you need as arguments, or define them in the later groups of the Thread(...items) form.
When a thread completes execution and is not continuously referenced, the system will automatically reclaim thread-related resources at the underlying level, without the need to explicitly call the join() function to release resources.
Supports backtesting system and live trading environment; all concurrent thread-related functions in the backtesting system are only provided for code compatibility support and will not actually execute concurrent threads.