[英]Multithreading executing multiple tasks in parallel in swift ios
I know creation of queues and able to execute single task but how can i execute multiple tasks in parallel. 我知道创建队列并能够执行单个任务,但是我如何并行执行多个任务。
Concurrent Queue ----> 并发队列---->
let concurrentQueue = DispatchQueue(label: "com.some.concurrentQueue", attributes: .concurrent)
concurrentQueue.async {
//executable code
}
BackgroundQueue without priorities default---> 没有优先级的BackgroundQueue默认为--->
DispatchQueue.global().async {
//executable code
}
Backgroundqueue with priorities----> 具有优先级的背景队列---->
DispatchQueue.global(qos: .userInitiated).async { //.userInteractive .background .default .unspecified
//executable code
}
Getting back to the main Queue ----> 回到主队列---->
DispatchQueue.main.async {
//executable code
}
All are asynchronous but how can i execute multiple methods at a time how should i code in swift. 所有这些都是异步的,但是如何一次执行多个方法,应该如何快速编写代码。
If you have a for loop method that calls a method and you want to calls this methods concurrent, so just use this : 如果您有一个调用方法的for循环方法,并且想同时调用此方法,则只需使用this:
DispatchQueue.concurrentPerform(iterations: Int, execute: { (count) in
doSomethingFor(count: count)
}
But if you have some individuals methods that you want to call concurrent, just do like this: 但是,如果您有一些要并发调用的个体方法,则可以这样:
let concurrentQueue = DispatchQueue(label: "com.some.concurrentQueue", attributes: .concurrent)
concurrentQueue.async {
//executable code
myFirstMethod()
}
concurrentQueue.async {
//executable code
mySecondMethod()
}
This way concurrentQueue, will manages your tasks concurrently itself. 这种方式的并发队列将自己并发地管理任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.