Grand Central Dispatch,不确定我是否完全理解这一点,这就是我的使用方式
Grand Central Dispatch, not sure if I am understanding this completely, here is how I am using it
我正在尝试掌握 Grand Central Dispatch 的窍门以提高我的代码的整体效率,但是,我并不完全确定如何使用它。下面是我当前对 GCD 的假设(可能是可怕的误导)的示例,基于教程以及许多其他堆栈溢出问题、一些 swift 博客和其他一些随机网站....
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.
}
我的假设是否正确?如果不是,你能告诉我哪里错了吗?为什么?如果您花时间阅读本文,非常感谢。
除了这个值得注意的例外:
你几乎所有的事情都是正确的
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
立即调用 return 而不会阻塞调用它们的线程。然而,调度例程的 concurrency 与该例程中的指令无关。请注意,闭包本质上只是捕获其作用域的函数,因此执行其内容时不存在任何独特的行为。
相反,并发发生在多个例程(即多个闭包)中,这些例程被分派到并发队列。采取以下措施:
// 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) }
我正在尝试掌握 Grand Central Dispatch 的窍门以提高我的代码的整体效率,但是,我并不完全确定如何使用它。下面是我当前对 GCD 的假设(可能是可怕的误导)的示例,基于教程以及许多其他堆栈溢出问题、一些 swift 博客和其他一些随机网站....
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.
}
我的假设是否正确?如果不是,你能告诉我哪里错了吗?为什么?如果您花时间阅读本文,非常感谢。
除了这个值得注意的例外:
你几乎所有的事情都是正确的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
立即调用 return 而不会阻塞调用它们的线程。然而,调度例程的 concurrency 与该例程中的指令无关。请注意,闭包本质上只是捕获其作用域的函数,因此执行其内容时不存在任何独特的行为。
相反,并发发生在多个例程(即多个闭包)中,这些例程被分派到并发队列。采取以下措施:
// 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) }