Thread
Thread objects can be created or returned through threading.Thread(), threading.getThread(), threading.mainThread(), threading.currentThread().
peekMessage
The peekMessage() function is used to receive messages from a thread.
peekMessage()
peekMessage(timeout)Examples
Concurrent thread sends messages to the main thread.
javascript
function main() {
var t1 = threading.Thread(function() {
for (var i = 0; i < 10; i++) {
Log("thread1 postMessage():", i)
threading.mainThread().postMessage(i)
Sleep(500)
}
})
while (true) {
var msg = threading.currentThread().peekMessage()
Log("main peekMessage():", msg)
if (msg == 9) {
break
}
Sleep(1000)
}
t1.join()
}Returns
| Type | Description |
string / number / bool / object / array / any (any type supported by the platform) | The |
Arguments
| Name | Type | Required | Description |
timeout | number | No | The |
See Also
Remarks
When writing programs, be careful to avoid thread deadlock issues.
postMessage
The postMessage() function is used to send messages to a thread.
postMessage(msg)Examples
-
Send messages in concurrent threads and use
eventLoop()to receive message notifications.javascriptfunction main() { var t1 = threading.Thread(function() { for (var i = 0; i < 10; i++) { Log("thread1 postMessage():", i) threading.mainThread().postMessage(i) Sleep(500) } }) for (var i = 0; i < 10; i++) { var event = threading.mainThread().eventLoop() Log("main event:", event) Sleep(500) } t1.join() } -
Supports sending functions.
javascriptfunction main() { threading.mainThread().postMessage(function(msg) { Log("func from mainThread, msg:", msg) }) threading.Thread(function() { var func = threading.mainThread().peekMessage() func("in " + threading.currentThread().name()) }).join() }
Arguments
| Name | Type | Required | Description |
msg | string / number / bool / object / array / function / any (any type supported by the platform) | Yes | The parameter |
See Also
Remarks
When the postMessage() function is called in a thread's execution function to send signals or data, it generates a message event. You can use the eventLoop() function to receive message notifications.
join
The join() function is used to wait for a thread to exit and reclaim system resources.
join()
join(timeout)Examples
Test join() function timeout and output the return value.
javascript
function main() {
var t1 = threading.Thread(function() {
Log("Hello thread1")
Sleep(5000)
})
var ret = t1.join(1000)
Log("ret:", ret) // ret: undefined
ret = t1.join()
Log("ret:", ret) // ret: {"id":1,"terminated":false,"elapsed":5003252000}
}Returns
| Type | Description |
| The
|
Arguments
| Name | Type | Required | Description |
timeout | number | No | The |
See Also
Remarks
When the join() function times out, it returns undefined.
terminate
The terminate() function is used to forcibly terminate a thread and release the hardware resources occupied when the thread was created.
terminate()Examples
Forcibly terminate the execution of a thread. After forcibly terminating the thread, the content output by that thread will no longer be displayed in the logs.
javascript
function main() {
var t1 = threading.Thread(function() {
for (var i = 0; i < 10; i++) {
Log("thread1 i:", i)
Sleep(1000)
}
})
Sleep(3000)
t1.terminate()
Log("after t1.terminate()")
while (true) {
LogStatus(_D())
Sleep(1000)
}
}See Also
Remarks
For threads forcibly terminated using the terminate() function, the join() function can no longer be used to wait for their completion.
getData
The getData() function is used to access variables recorded in the thread environment. The data is valid when the thread has not executed the join() function (waiting for successful exit) and has not executed the terminate() function (forcibly terminating the thread).
getData()
getData(key)Examples
Record a value with the key name count in the concurrent thread environment, then read the key value of count in the main thread.
javascript
function main() {
var t1 = threading.Thread(function() {
for (var i = 0; i < 5; i++) {
threading.currentThread().setData("count", i)
Log(`setData("count"):`, i)
Sleep(1000)
}
})
for (var i = 0; i < 5; i++) {
var count = threading.getThread(t1.id()).getData("count")
Log(`getData("count"):`, count)
Sleep(1000)
}
t1.join()
}Returns
| Type | Description |
string / number / bool / object / array / any (any type supported by the platform) | The |
Arguments
| Name | Type | Required | Description |
key | string | Yes | The |
See Also
setData
The setData() function is used to store variables in the thread environment.
setData(key, value)Examples
-
Set a key-value pair in a concurrent thread and read the key-value pair in the main thread.
javascriptfunction main() { var t1 = threading.Thread(function() { threading.currentThread().setData("data", 100) }) Sleep(1000) Log(`t1.getData("data"):`, t1.getData("data")) t1.join() } -
Supports passing functions as key values.
javascriptfunction main() { threading.mainThread().setData("func2", function(p) { Log("func2 p:", p) }) var t1 = threading.Thread(function() { threading.currentThread().setData("func1", function(p) { Log("func1 p:", p) }) var func2 = threading.mainThread().getData("func2") func2("test2") }) Sleep(1000) var func1 = t1.getData("func1") func1("test1") t1.join() }
Arguments
| Name | Type | Required | Description |
key | string | Yes | The |
value | string / number / bool / object / array / function / any (any type supported by the platform) | Yes | The |
See Also
Remarks
Data remains valid as long as the thread has not executed the join() function (waiting for successful exit) and has not executed the terminate() function (forcibly terminating the thread). The value parameter must be a serializable variable.
id
The id() function is used to return the threadId of the current multi-threaded object instance.
id()Examples
Create a concurrently running thread and output the threadId of that concurrent thread in the main thread.
javascript
function main() {
var t1 = threading.Thread(function() {
threading.currentThread().setData("data", 100)
})
Log(`t1.id():`, t1.id())
t1.join()
}Returns
| Type | Description |
number | The |
See Also
name
The name() function is used to return the name of the current multi-threaded object instance.
name()Examples
Create a concurrently running thread and output the name of that concurrent thread in the main thread.
javascript
function main() {
var t1 = threading.Thread(function() {
threading.currentThread().setData("data", 100)
})
Log(`t1.name():`, t1.name()) // t1.name(): Thread-1
t1.join()
}Returns
| Type | Description |
string | The |
See Also
eventLoop
The eventLoop() function is used to listen for events received by the thread.
eventLoop()
eventLoop(timeout)Examples
Execute 3 threads concurrently and output the received event information. When timeout occurs or immediate return, the output value is null.
javascript
function main() {
var t1 = threading.Thread(function() {
while (true) {
var eventMsg = threading.currentThread().eventLoop() // 阻塞等待
// 2024-11-14 10:14:18 thread1 eventMsg: {"Seq":1,"Event":"thread","ThreadId":0,"Index":1,"Queue":0,"Nano":1731550458699947000}
Log(_D(), "thread1 eventMsg:", eventMsg)
}
})
var t2 = threading.Thread(function() {
while (true) {
var eventMsg = threading.currentThread().eventLoop(-1) // 立即返回
Log(_D(), "thread2 eventMsg:", eventMsg)
Sleep(5000)
}
})
var t3 = threading.Thread(function() {
while (true) {
var eventMsg = threading.currentThread().eventLoop(3000) // 设置3秒超时
Log(_D(), "thread3 eventMsg:", eventMsg)
}
})
t1.postMessage("Hello ", t1.name())
t2.postMessage("Hello ", t2.name())
t3.postMessage("Hello ", t3.name())
t1.join()
t2.join()
t3.join()
}Returns
| Type | Description |
object / null | The |
Arguments
| Name | Type | Required | Description |
timeout | number | No | The parameter |
See Also
Remarks
The processing mechanism of the eventLoop() function is consistent with the global function EventLoop().