ios - Grand Central Dispatch,不确定我是否完全理解这一点,这就是我的使用方式

标签 ios swift concurrency grand-central-dispatch synchronous

我正在尝试掌握 Grand Central Dispatch 的窍门以提高我的代码的整体效率,但是,我并不完全确定如何使用它。下面是我当前对 GCD 的假设(可能是可怕的误导)的示例,基于教程以及许多其他堆栈溢出问题、一些快速的博客和其他一些随机网站....

 dispatch_async(dispatch_get_main_queue()){
    //this will execute the task wihtin this closure, asynchonously, meaning 
    //that it does not block the thread, yet the code within the closure will       
    //be executed serailly, meaning each line must finish before proceeding
print(1)
print(2)
print(3)
print(4)
//this will print 1,2,3,4, in that exact order, everytime. But since it is 
//called asynchronously it returns immediatley after initiating the task, and 
//thus does not block the UI from being interacted with
}

dispatch_sync(dispatch_get_main_queue()){
 //this will execute the taks within the closure synchronously, meaning that it WILL block
 //the current thread until the task is completed at which point it will return and make the
//thread available again. The main queue is a serial queue, so each line will be started
//and finish before the line after it is called.
  print(1)
  print(2)
  print(3)
  print(4)
  //this will print 1,2,3,4, in that exact order, everytime. Because it is called synchronously
  //and on the main queue, it will block the UI. The UI will not become interactive again
  //until this function returns, and it will not return until "4" is printed.
}

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.rawValue), 0)){
  //this will execute the task within the closure asycnrhonously, meaning that
  //it does not block the current thread and will return before completion of all its contents.
 //As well, this is executed on a concurrent queue, so the lines within will begin 
 //in the order they are shown however they may finish at any time
  print(1)
  print(2)
  print(3)
  print(4)
  //this could print the 4 numbers in ANY ORDER. Since it is called asynchronously it returns immediatley,
 //and does not block the current thread. And because the global_queue is a
 //concurrent queue, the numbers could print in any order
}


dispatch_sync(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.rawValue), 0)){
 //this will execute the tasks within synchronously, meaning that it WILL block the 
//current thread until the task is completed. It is dispatched to a concurrent queue, 
//therefore the tasks within will each begin in the order that they are read,
//however they may finish at any time
  print(1)
  print(2)
  print(3)
  print(4)
  //this could print 1,2,3,4 in ANY ORDER. This WILL block the current thread since 
 //it is called synchronously. However, since it is being called on a concurrent queue,
 //it could print the numbers in any order.
}

我的假设是否正确?如果不是,你能告诉我哪里错了吗?为什么?如果您花时间阅读本文,非常感谢。

最佳答案

除了这个明显的异常(exception),你几乎所有的事情都是正确的:

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.rawValue), 0)){
    /*
     ...
     this is executed on a concurrent queue, so the lines within will begin 
     in the order they are shown however they may finish at any time
     ...
     this could print the 4 numbers in ANY ORDER.
     ...
     because the global_queue is a
     concurrent queue, the numbers could print in any order
    */
}

你对异步方面的理解是正确的; async 调用立即返回,不会阻塞调用它们的线程。但是,分派(dispatch)例程的并发性与该例程中的指令无关。请注意,闭包本质上只是捕获其作用域的函数,因此执行其内容时不存在任何独特的行为。

相反,并发发生在分派(dispatch)到并发队列的多个例程(即多个闭包)之间。采取以下措施:

// These routines are executed asynchronously to the current thread, and
// serially to one another; the results will always be 1 through 4.
let serialQueue = dispatch_get_main_queue()
dispatch_async(serialQueue) { print(1) }
dispatch_async(serialQueue) { print(2) }
dispatch_async(serialQueue) { print(3) }
dispatch_async(serialQueue) { print(4) }

// These routines are executed asynchronously to the current thread, and
// concurrently to one another; they will print out in an undefined order.
let concurrentQueue = dispatch_get_global_queue(Int(QOS_CLASS_UTILITY.rawValue), 0)
dispatch_async(concurrentQueue) { print(1) }
dispatch_async(concurrentQueue) { print(2) }
dispatch_async(concurrentQueue) { print(3) }
dispatch_async(concurrentQueue) { print(4) }

关于ios - Grand Central Dispatch,不确定我是否完全理解这一点,这就是我的使用方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35123488/

相关文章:

ios - 从 HTML 正文创建 PDF 文件并作为邮件附件发送,iOS

ios - 在 Swift 中从 App 向 watchOS 发送消息

ios - SwiftUI:删除ForEach中的最后一行

java - ConcurrentHashmap 在我的实现中是否需要同步?

ios - 如何为JSON字典添加值?

ios - MKOverlayPathRenderer 反转填充区域

ios - 在 Swift 中将字符串转换为 NSObject

swift - 在类中创建自定义操作以在 Interface Builder 中使用

go - 为什么互斥锁比 golang 中的 channel 慢?

php 和 mySQL 数据库并发更新