gpt4 book ai didi

multithreading - NSOperation 会产生一个新线程吗?

转载 作者:行者123 更新时间:2023-11-28 19:24:02 25 4
gpt4 key购买 nike

很简单,线程(或 NSThread)和 NSOperation 之间是否存在一对一的连接?或者它是否抽象出一个操作是一种可以在后台由多个线程获取和运行的任务?

最佳答案

不是一对一的连接,不是。使用 NSOperation 子类的优点是您不需要自己管理多线程。 Apple 甚至(令人困惑地)将典型的 NSOperation 子类(即重写 -main 方法的子类)定义为非并发,而不是因为它没有'不支持并发,但是因为并发的细节由父类(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.

从这个意义上说,NSOperation 子类更像是 NSThread 的目标对象detachNewThreadSelector:toTarget:withObject:.

如果您想控制并发的行为方式,另一种方法是覆盖 -start 并在调用 -main 之前根据需要设置并发。

这是一个非常好的概述:Managing Concurrency with NSOperation

关于multithreading - NSOperation 会产生一个新线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5979219/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com