搞了好几遍,之前的思考不是很清楚,这次算是搞清楚了,
具体参考:http://stackoverflow.com/questions/1646795/nsoperationqueue-and-concurrent-vs-non-concurrent
NSOperationQueue
always executes operations concurrently, while taking dependencies into account.
A "non-concurrent" operation requires a separate thread in order to execute concurrently.NSOperationQueue
is responsible for providing this thread. In other words, a non-concurrent operation depends on NSOperationQueue
to make it a concurrent operation.
A "concurrent" operation is concurrent on its own; it doesn't need NSOperationQueue
to create a thread for it. An example would be an operation that uses asynchronous file IO.
If you want two or more operations to execute serially you need to use dependencies.
If you want an operation to block the main thread then don't use NSOperationQueue
; just run each operation one after the other on the main thread.
1:非并发时,queue启动的operation子类时,创建线程,在创建的线程中执行main函数中的方法。每一个operation都是不相同的线程。
2:并发时,queue启动operation子类时,创建线程,在创建的线程中异步执行start方法中的完毕异步方法,而这个时候需要注意了,start瞬间执行完毕,而需要处理的异步还没返回,所以需要启动线程的runloopmodel。但这样会影响后面的operation(比如你设置了最大的operation数)。因此比较流行的做法,如AFNetWoking 的AFURLConnectionOperation,创建一个单例线程,该线程启动runloopmodel,所有的异步都在该线程中执行。
上面说了具体操作,具体区别方面,上面的英文说的很清楚:
非并发的依靠operationqueue提供一个线程,同时管理该线程,使之成为并发的。
而并发的不需要NSOperationQueue为他穿件线程,需要自己创建线程处理自己的异步逻辑。这里面疑问是不需要为并发的创建线程,但是断电在start中发现,每次执行start的线程都不一样,因此start本身在NSOperationQueue创建的线程中执行。而且每一个start都分布在不同的线程。