问题描述
假设我有一个使用getChildren()
方法的节点,该节点进行API调用以获取其子节点。
我想用修补程序号提取整个树(到节点列表中)。线程。我想使用NSThreads来降低延迟(我从文档中读到的东西是在GCD上使用线程的原因。)
到目前为止,我已经提出了以下建议:
static func queryElementRecursive(rootElement: Node) -> [Node] {
let threadCount = 30
let queueLock = NSLock.init()
let outputLock = NSLock.init()
var output: [Node] = []
var queue: [Node] = [rootElement]
for thread in 1...threadCount {
let thread = Thread.init(block: {
while true {
queueLock.lock()
let eOptional = queue.popLast()
queueLock.unlock()
guard let e = eOptional else {
// I can't break here and kill the thread because I don't kNow if more children may be enqueued later
continue
}
outputLock.lock()
output.append(e)
outputLock.unlock()
let childrenoptional: [Node]? = try? e.getChildren()
if let children = childrenoptional {
queueLock.lock()
queue.append(contentsOf: children)
queueLock.unlock()
}
}
})
thread.start()
}
// how do i wait for the tree traversal to complete??
return output
}
我面临的问题是不知道遍历何时完成以及线程何时可以退出。我不能空着队列退出,因为每个作业都可以排队更多的作业。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)