温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

浅谈NSOperation的并发与非并发

发布时间:2020-06-26 23:35:24 来源:网络 阅读:466 作者:zhaoguohui000 栏目:开发技术

Operation分为并发和非并发两种,也即相对caller线程是同步的还是异步的。对于提交到Operation queue中的operation来说,都是异步的,但是如果手动启动operation的话,就有异步和非异步的区分了。


Although you typically execute operations by adding them to an operation queue, doing so is not required. It is also possible to execute an operation object manually by calling its start method, but doing so does not guarantee that the operation runs concurrency with the rest of your code. The isConcurrent method of the NSOperation class tells you whether an operation runs synchronously or asynchrously with respect to the thread in which its start method was called. By default, this method returns NO, which means the operation runs synchronously in the calling thread.


If you want to implement a concurrent operation - that is, one that runs asynchronously with respect to the calling thread - you must write additional code to start the operation asynchronously. For example, you might spawn a separate thread, call an asynchronous system function, or do anything else to ensure that the start method starts the task and returns immediately and, in all likeihood, before the task is finished.


Most developers should never need to implement concurrent operation objects. If you always ad your operations to an operation queue, you do not need to implement concurrent operations. When you submit a nonconcurrent operation to an operation queue, the queue itself creates a thread on which to run your operation. Thus, adding a nonconcurrent operation to an operation queue still results in the asynchronous execution of your operation object code. The ability to define concurrent operations is only necessary in cases where you need to execute the operation asynchronously without adding it to an operation queue.


For information about how to create a concurrent operation, see "Configuring Operations for Concurrent Execution" and NSOperation Class Reference.







向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI