浅谈NSOperation的并发与非并发
2014-12-26 18:07
281 查看
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.
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.
相关文章推荐
- NSOperation的最大并发数量,挂起,依赖的操作
- 浅谈Java两种并发类型——计算密集型与IO密集型
- NSOperation的start与main,并发与非并发。
- 浅谈千万级PV/IP规模高性能高并发网站架构
- 浅谈千万级PV/IP规模高性能高并发网站架构
- 浅谈千万级PV/IP规模高性能高并发网站架构(转自老男孩)
- 浅谈并发服务器----多线程并发----2
- 深入理解计算机系统(1.4)---并发与并行、浅谈抽象
- 关于数据加锁和高并发的浅谈
- iOS多线程编程——浅谈NSOperation
- iOS并发编程(二)——NSOperation
- 第02天多线程网络:(12):NSOperation的其他用法(暂停恢复取消最大并发数)
- 浅谈Java线程并发知识点
- 【分享】浅谈并发下的HashMap
- 浅谈如何提高服务器并发处理能力
- 深入理解计算机系统(1.4)---并发与并行、浅谈抽象
- 浅谈并发与并行
- 浅谈千万级PV/IP规模高性能高并发网站架构
- 浅谈千万级PV/IP规模高性能高并发网站架构
- 浅谈Linux环境下并发编程中C语言fork()函数的使用