核心数据并发(NSOperation)

发布于 2024-10-21 03:01:42 字数 698 浏览 5 评论 0原文

在Apple docs 中写道:

...您应该在中创建上下文 main(对于串行队列)或start (对于并发队列)。

但我真的不明白有什么区别。为什么我无法在 main 中为并发队列创建上下文?我尝试过,它的工作原理与我在 start 中所做的完全一样。

还有一件事让我很困惑。来自 启动方法:

...如果您正在实施 并发操作,你必须 覆盖此方法并使用它 启动您的操作。

那么,为什么我不能初始化 main 中的所有内容(或者也许不应该初始化)?

In Apple docs it is written:

...you should create the context in
main (for a serial queue) or start
(for a concurrent queue).

But I really don't get what is the difference. Why can't I create context in main for the concurrent queue? I tried that and it works absolutely the same as if I did that in start.

There is also another thing that confuses me. From docs for the start method:

...If you are implementing a
concurrent operation, you must
override this method and use it to
initiate your operation.

So again, why can't I initialize everything in main (or maybe should not initialize)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒夜笙歌凉 2024-10-28 03:01:42

您所说的“上下文”和“并发”与 Apple 对这些术语的含义可能有所不同。他们以一种特定的(并且令人困惑的)方式使用“并发”。 使用 NSOperation 管理并发 中的这一部分可能会有所帮助:

使用 NSOperation 有多种不同的方法,但最常见的是编写自定义子类并重写一个方法:main。当 NSOperationQueue 安排它运行时,将调用 main 方法来执行操作。以这种方式编写的 NSOperation 类被称为非并发操作,因为开发人员不负责生成线程——多线程全部由超类处理。 (不要被术语混淆:仅仅因为一个操作是非并发的,并不意味着它不能并发执行,它只是意味着您不必自己处理并发性。)

如果您需要对线程和操作的运行时环境进行更多控制,则可以使用并发操作。为此,您需要子类化 NSOperation 并重写 start 方法。在 start 方法中,您可以在调用 main 方法之前生成线程并设置环境。您还需要通过设置 isExecuting 和 isFinished 等属性来维护 NSOperation 的状态。简而言之,并发操作为您提供了更多的控制权,但也需要更多的努力 - 对于大多数任务,非并发操作就足够了。

There may be a difference between what you're calling 'context' and 'concurrent' and what Apple means by the terms. They use 'concurrent' in a specific (and confusing) way. This section from Managing Concurrency With NSOperation might help:

There are a number of different ways that you can use NSOperation, but the most common is to write a custom subclass and override one method: main. The main method gets called to perform the operation when the NSOperationQueue schedules it to run. NSOperation classes written in this way are known as non-concurrent operations, because the developer is not responsible for spawning threads—multi-threading is all handled by the super class. (Don’t be confused by the terminology: just because an operation is non-concurrent, does not mean it cannot be executed concurrently, it simply means that you don't have to handle the concurrency yourself.)

If you need more control over threading and the run-time environment of your operations, you can make use of concurrent operations. To do this, you subclass NSOperation and override the start method. In the start method, you can spawn threads and setup the environment before calling the main method. You are also required to maintain the state of the NSOperation by setting properties like isExecuting and isFinished. In short, concurrent operations give you a lot more control, but also demand more effort—for most tasks non-concurrent operations suffice.

私藏温柔 2024-10-28 03:01:42

没有理由不能在 main() 中为并发队列创建上下文,但由于您必须在 main() 中管理运行循环,因此如果您使用它,通常最好在 start() 中设置诸如上下文之类的东西技术。

There's no reason you can't create the context in main() for a concurrent queue, but since you have to manage a run loop in main it's usually nicer looking to set up things like a context in start() if you are using that technique.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文