The setData()
function is used to store variables in the thread context.
setData(key, value)
The key
parameter is used to specify the key name of the stored key-value pair.
key
true
string
The value
parameter is used to specify the key value of the stored key-value pair.
value true Any type supported by the system, such as string, number, bool, object, array, function, null value, etc.
function main() {
var t1 = threading.Thread(function() {
threading.currentThread().setData("data", 100)
})
Sleep(1000)
Log(`t1.getData("data"):`, t1.getData("data"))
t1.join()
}
Set the key-value pair in the concurrent thread and read the key-value pair in the main thread.
function 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()
}
It supports key-value passing into functions.
The data is valid when the thread has not executed the join()
function (waiting for exit success) and has not executed the terminate()
function (terminating the thread forcibly). The value of the parameter value
must be a serializable variable.
{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/postMessage postMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}
getData id