如何通过NSOperation实现并发任务? [英] How to achieve concurrent task through NSOperation?

查看:146
本文介绍了如何通过NSOperation实现并发任务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 NSoperation 的新手。 NSoperation 是一个单一对象。

I am new to NSoperation. NSoperation is a singleshot object.


  1. 我们如何实现多个操作同时通过 NSoperation

可以不用 NSoperationQueue

即使我们使用NSoperationQueue,它也会执行操作FIFO格式。它会如何并发执行?

Even if we use NSoperationQueue, it will perform operations FIFO format.How will it execute Concurrently?


推荐答案

如果你想实现并发操作 - 那就是运行一个与调用线程异步 - 您必须编写其他代码以异步启动操作。例如,您可能生成一个单独的线程,调用异步系统函数,或者执行任何其他操作以确保start方法启动任务并立即返回,并且很可能在任务完成之前返回。

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 likelihood, before the task is finished.

大多数开发人员永远不需要实现并发操作对象。 如果始终将操作添加到操作队列,则无需实现并发操作。向操作队列提交非并发操作时,队列本身会创建一个运行操作的线程。因此,向操作队列添加非并发操作仍会导致操作对象代码的异步执行。只有在需要异步执行操作而不将其添加到操作队列的情况下,才需要定义并发操作。

Most developers should never need to implement concurrent operation objects. If you always add 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.

参见并发编程指南 - 并发与非并发操作一节

另请阅读使用NSOperation管理并发

您通常通过将操作添加到<来执行操作strong>操作队列(NSOperationQueue类的一个实例)。

You typically execute operations by adding them to an operation queue (an instance of the NSOperationQueue class).

NSOperationQueue 类调节一组NSOperation的执行对象。添加到队列后,操作将保留在该队列中,直到明确取消或完成其任务为止。 队列中的操作(但尚未执行)本身是根据优先级和操作间对象依赖关系组织的,并相应地执行。应用程序可以创建多个操作队列并向其中任何一个提交操作。

The NSOperationQueue class regulates the execution of a set of NSOperation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task. Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly. An application may create multiple operation queues and submit operations to any of them.

操作队列直接执行操作,在辅助线程上运行它们,或间接使用libdispatch库

An operation queue executes its operations either directly, by running them on secondary threads, or indirectly using the libdispatch library

您可以阅读更多关于 NSOperation 并详细了解 NSOperationQueue

You can read more about NSOperation here and read more about NSOperationQueue here

这篇关于如何通过NSOperation实现并发任务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆