NSOperation concurrent vs non-concurrent

搞了好几遍,之前的思考不是很清楚,这次算是搞清楚了,

具体参考: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.

之前只清除operationqueue 启动operation时会创建线程,没有搞清楚并发和非并发的区别。


1:非并发时,queue启动的operation子类时,创建线程,在创建的线程中执行main函数中的方法。每一个operation都是不相同的线程。


2:并发时,queue启动operation子类时,创建线程,在创建的线程中异步执行start方法中的完毕异步方法,而这个时候需要注意了,start瞬间执行完毕,而需要处理的异步还没返回,所以需要启动线程的runloopmodel。但这样会影响后面的operation(比如你设置了最大的operation数)。因此比较流行的做法,如AFNetWoking 的AFURLConnectionOperation,创建一个单例线程,该线程启动runloopmodel,所有的异步都在该线程中执行。

上面说了具体操作,具体区别方面,上面的英文说的很清楚:

非并发的依靠operationqueue提供一个线程,同时管理该线程,使之成为并发的。

而并发的不需要NSOperationQueue为他穿件线程,需要自己创建线程处理自己的异步逻辑。这里面疑问是不需要为并发的创建线程,但是断电在start中发现,每次执行start的线程都不一样,因此start本身在NSOperationQueue创建的线程中执行。而且每一个start都分布在不同的线程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值