threading.Dict
创建一个字典对象,用于在并发线程间传递和共享数据。
Dict()示例
-
向并发线程执行函数传入普通对象,测试修改对象键值后是否会影响其他线程中的对象键值。
javascriptfunction threadFun1(obj) { obj["age"] = 100 while (true) { Log("threadFun1 obj:", obj) Sleep(5000) } } function threadFun2(obj) { while (true) { Log("threadFun2 obj:", obj) Sleep(5000) } } function main() { var obj = {"age": 10} var t1 = threading.Thread(threadFun1, obj) var t2 = threading.Thread(threadFun2, obj) t1.join() t2.join() } -
向并发线程执行函数传入通过
Dict()函数创建的ThreadDict对象,测试修改对象键值后是否会影响其他线程中的对象键值。javascriptfunction threadFun1(threadDict) { threadDict.set("age", 100) while (true) { Log(`threadFun1 threadDict.get("age"):`, threadDict.get("age")) Sleep(5000) } } function threadFun2(threadDict) { while (true) { Log(`threadFun2 threadDict.get("age"):`, threadDict.get("age")) Sleep(5000) } } function main() { var threadDict = threading.Dict() threadDict.set("age", 10) var t1 = threading.Thread(threadFun1, threadDict) var t2 = threading.Thread(threadFun2, threadDict) t1.join() t2.join() }
返回值
| 类型 | 描述 |
|
参考
备注
向并发线程函数传入普通对象时采用深拷贝方式传递,在并发线程中修改键值不会影响其他线程中的字典。
支持回测系统、实盘环境。