iOS 中NSOperationQueue,Grand Central Dispatch , Thread的上下关系和区别
In OS X v10.6 and later, operation queues use the libdispatch
library (also known as Grand Central Dispatch) to initiate the execution of their operations. As a result, operations are always executed on a separate thread, regardless of whether they are designated as concurrent or non-concurrent operations.
In iOS 4 and later, operation queues use Grand Central Dispatch to execute operations.
这2句话是NSOperationQueue Class Reference中的,这2句话表明NSOperationQueue是利用了GCD机制的,而Thread一直是最基本的多线程机制,那么它们3者的上下层关系便出来了:
Thread最基本,其次是GCD,封装的最高层是operation quque。在遇到多线程问题时,推荐使用Operation Queue。最新的AFNetWorking库也使用Operation Queue代替了GCD,代码开起来更简洁了。
那么这3中多线程方式具体的区别在哪里呢?
首先说NSThread,这个是系统最基本的多线程机制,一切其他机制都是由它衍生出来的。既然是最基本的,那么用起来也一定最复杂:它仅仅提供了最基本的线程机制,不会提供那些同步异步的各种机制。
其实,关键是区别CGD和NSOperationQueue 的区别。