NSOperationQueue和并发vs非并发 [英] NSOperationQueue and concurrent vs non-concurrent

查看:127
本文介绍了NSOperationQueue和并发vs非并发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用NSOperationQueue设置一个序列化任务队列,但我对文档中讨论的术语感到困惑。

I want to setup a serialized task queue using NSOperationQueue but I'm a little confused by the terminology discussed in the documentation.


在NSOperation
对象的上下文中,术语concurrent和
非并发不一定是
指的是
线程的并行执行。相反,非并发
操作是使用
执行的环境为
提供的环境,而并发操作是
负责设置自己的
执行环境。

In the context of an NSOperation object, the terms concurrent and non-concurrent do not necessarily refer to the side-by-side execution of threads. Instead, a non-concurrent operation is one that executes using the environment that is provided for it while a concurrent operation is responsible for setting up its own execution environment.

设置自己的执行环境是什么意思?

What does it mean to setup "own execution environment'?

我的NSOperation派生任务需要按他们被添加到队列的顺序执行。

My NSOperation derived tasks need to execute serially in the order they were added to the queue.

所以我认为这意味着一个非并发操作我将实现'main'用于需要完成的工作,并且为'isConcurrent'返回NO。此外,NSOperationQueue的'setMaxConcurrentOperationCount'将被设置为1.

So I thought that this implies a 'non-concurrent' operation so I'd implement 'main' for the work that needs to be completed and also return NO for 'isConcurrent'. In addition, NSOperationQueue's 'setMaxConcurrentOperationCount' would be set to 1.

我没有在这些任务之间设置NSOperation依赖的原因是因为它们插入队列的顺序是他们应该完成的顺序。

The reason I am not setting up NSOperation dependency between these tasks is because the order they're inserted into the queue is the order they should be completed.

这些假设正确?

推荐答案

NSOperationQueue 总是同时执行操作,

NSOperationQueue always executes operations concurrently, while taking dependencies into account.

非并发操作需要单独的线程才能同时执行。 NSOperationQueue 负责提供此线程。换句话说,非并发操作取决于 NSOperationQueue ,使其成为并发操作。

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.

操作本身是并发的;它不需要 NSOperationQueue 为它创建一个线程。一个例子是使用异步文件IO的操作。

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.

如果你想要一个操作阻塞主线程,那么不要使用 NSOperationQueue ;只是在主线程上一个接一个地运行每个操作。

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 setMaxConcurrentOperationCount:

To manually set maximum of concurrent operations, use method on operationQueue setMaxConcurrentOperationCount:

这篇关于NSOperationQueue和并发vs非并发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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