Type/to search
Built-in Functions
Global
Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
exchange.Go
EventLoop
ctx.subscribe
ctx.poll
ctx.order
ctx.cancel
ctx.submit
Subscription
threading
threading.Thread
threading.getThread
threading.mainThread
threading.currentThread
threading.Lock
threading.Condition
threading.Event
threading.Dict
threading.Serve
threading.pending
Thread
ThreadLock
ThreadEvent
ThreadCondition
ThreadDict
Server
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

Creates concurrent threads.

Thread(func, ...args)
Thread(...items)

Examples

  • Create a concurrent thread with both a custom function and an anonymous function simultaneously.

    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() }
  • 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.

    javascript
    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() }
  • Support passing functions as parameters to concurrently executing functions.

    javascript
    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() }
  • Support passing function strings to dynamically import external libraries for concurrent computation.

    javascript
    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,"error":null,"ret":{"0":0.9339330196380615}} Log(ret) }

Returns

TypeDescription

Thread object

Thread object for managing created concurrent threads, thread communication, etc.

Arguments

NameTypeRequiredDescription

func

function

Yes

The parameter func is a function for concurrent execution (passed by reference), supporting anonymous functions. func can accept multiple parameters, which will be passed through ...args during concurrent execution. Therefore, the parameter list of func needs to be consistent with ...args.

arg

string / number / bool / object / array / function / any

No

The parameter arg is the actual parameter passed to func (the concurrent thread execution function) during callback execution; there can be multiple arg parameters, and the parameter list of func needs to be consistent with ...args.

item

array

Yes

The parameter item is an array whose first element is a function reference or a function source string and whose remaining elements are that function's arguments. When calling the Thread function, multiple sets of item parameters can be passed. With several sets they run in one thread in the following order, from the last set backwards to the second set, then the first set last; the first set is the main function, and its return value becomes ret in the join() result. The later sets are usually used to define functions or load libraries; a source string is evaluated as a global script, the functions it defines can be called from the main function, and it takes no arguments.

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.