iphone/objective-c dev 中有多少个线程太多了

发布于 2024-10-31 22:03:25 字数 481 浏览 5 评论 0原文

我有一个复杂的同步作业,它通过 HTTP 对内容进行多次异步调用。每次收到此内容时,它都会询问下一位,依此类推。这些都与服务器上的数据以菊花链方式连接在大型整体同步作业中。

这个工作链大概有 12 个步骤。在大约第 5 个异步请求之后,它似乎陷入了困境,该请求永远不会返回,并且永远挂起等待它。我认为这可能与生成的线程太多有关,因为如果我关闭它在开始时挂起的线程,它会正常返回。

按照我在脑海中想象的方式,主线程请求异步内容 a。当它以自己的异步时间返回时,它会生成一个新线程,然后该线程请求 aync 内容 b。当它在自己的甜蜜时光回来时,它会生成一个新线程,然后该线程请求内容 c。每次异步请求返回结果时不是都会创建一个新线程吗?

我以菊花链方式连接这些请求是否正确?我非常擅长 Java 开发中的线程,但我对它们在 Obj-C 中的工作方式有点困惑。我是否需要使用 3 个线程的线程池并重用它们?

很抱歉问了这个高层次的问题,但我相信一些专家可以帮助消除围绕这个问题的神秘云彩。

I have a complex sync job that does several asyncronous calls for content over HTTP. Each time this content is received, it asks for the next bit and so on. These are all daisey-chained in a big over-all sync job with data on the server.

There are probably 12 steps in this job chain. It seems to get stuck after around the 5th async request, the request never comes back and it hangs for ever waiting for it. I think it may have to do with too many threads being spawned because if I fire off the one it hangs at at the beginning it returns fine.

In the way I imagine it in my head, the main thread asks for async content a. When it comes back in its own asynchronous time it spawns a new thread which then asks for aync content b. When it comes back in its own sweet time it spawns a new thread which then asks for content c. Isn't a new thread being created everytime an async request returns a result?

Am I daisy-chaining these requests right? I was quite good at threads in Java development but I'm a bit confused on how they work in Obj-C. Do I need to use a Thread pool of say 3 threads and reuse these?

Sorry for the high-level question but I'm sure some experts can help clear the cloud of mystery around this.

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

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

发布评论

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

评论(1

稳稳的幸福 2024-11-07 22:03:25

NSOperationQueues 构建在 Grand Central Dispatch 之上。如果您需要精确控制操作顺序以及分派同步请求的能力,您可能需要直接使用 GCD。使用任何一个,您都不需要担心线程创建/管理。您只需根据应用程序的需要对操作进行排队即可。

恕我直言,Apple 文档对此很好,但您可以在那里找到许多教程。

[编辑:添加了Apple文档的链接]

http ://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html

NSOperationQueues are built on top of Grand Central Dispatch. If you need precise control over order of operations and the ability to dispatch synchronous requests you might want to use GCD directly. Using either, you don't really need to worry about thread creation/management. You simply queue your operations as needed by your app.

The Apple docs are fine on this IMHO but you can find a number of tutorials out there.

[EDIT: added link to Apple docs]

http://developer.apple.com/library/ios/#documentation/General/Conceptual/ConcurrencyProgrammingGuide/Introduction/Introduction.html

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