Objective-C、NSThread 分离与 PerformSelectorInBackground

发布于 2024-10-25 08:39:34 字数 470 浏览 1 评论 0原文

这两者有什么区别?

 [NSThread detachNewThreadSelector:@selector(method) toTarget:self withObject:nil];
 [self performSelectorInBackground:@selector(method) withObject:nil];

我通常使用第二种方法来生成一个新线程。 但我想知道如果我在一个方法中像下面所示那样调用它两次,那么会发生什么?另外,如果我有一个选项卡菜单,并且每个菜单都会产生一个线程,那么我应该使用哪一个?

 [self performSelectorInBackground:@selector(method1) withObject:nil];
 [self performSelectorInBackground:@selector(method2) withObject:nil];

What is the differences between these two?

 [NSThread detachNewThreadSelector:@selector(method) toTarget:self withObject:nil];
 [self performSelectorInBackground:@selector(method) withObject:nil];

I normally use the second method to spawn a new thread.
But I was wondering if I call this twice like shown below in a method then what is going to happen? Also If I have a tabmenu and each menu spawns a thread then which one I should use?

 [self performSelectorInBackground:@selector(method1) withObject:nil];
 [self performSelectorInBackground:@selector(method2) withObject:nil];

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

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

发布评论

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

评论(1

梦旅人picnic 2024-11-01 08:39:35

它们是相同的。这是官方文档 关于这个话题不得不说:


iOS 和 Mac OS X v10.5 及更高版本,全部
物体有能力产生
新线程并用它来执行一个
他们的方法。这
执行SelectorInBackground:withObject:
方法创建一个新的分离线程
并使用指定的方法作为
新线程的入口点。为了
例如,如果你有一些对象
(由变量 myObj 表示)
该对象有一个名为的方法
doSomething 你想在 a 中运行
后台线程,你可以使用
下面的代码可以做到这一点:

[myObj
PerformSelectorInBackground:@selector(doSomething)
withObject:nil];

调用的效果
这个方法和你一样
称为
分离NewThreadSelector:toTarget:withObject:
NSThread 的方法与当前
对象、选择器和参数对象
作为参数。新线程是
使用默认值立即生成
配置并开始运行。
在选择器内部,您必须
像您一样配置线程
任何线程。例如,你会
需要设置一个自动释放池(如果
你没有使用垃圾收集)
并配置线程的运行循环 if
你计划使用它。供参考
关于如何配置新线程,请参见
“配置线程属性。”

至于如果这样做会发生什么:

[self performSelectorInBackground:@selector(method1) withObject:nil];
[self performSelectorInBackground:@selector(method2) withObject:nil];

...您将生成两个新线程,其中一个在 method1 处开始执行,其中一个在 method2 处开始执行。线程可以同时执行(即第二个线程在开始执行之前不会等待第一个线程终止)。

They are identical. Here is the what the official documentation has to say on this topic:

In
iOS and Mac OS X v10.5 and later, all
objects have the ability to spawn a
new thread and use it to execute one
of their methods. The
performSelectorInBackground:withObject:
method creates a new detached thread
and uses the specified method as the
entry point for the new thread. For
example, if you have some object
(represented by the variable myObj)
and that object has a method called
doSomething that you want to run in a
background thread, you could could use
the following code to do that:

[myObj
performSelectorInBackground:@selector(doSomething)
withObject:nil];

The effect of calling
this method is the same as if you
called the
detachNewThreadSelector:toTarget:withObject:
method of NSThread with the current
object, selector, and parameter object
as parameters. The new thread is
spawned immediately using the default
configuration and begins running.
Inside the selector, you must
configure the thread just as you would
any thread. For example, you would
need to set up an autorelease pool (if
you were not using garbage collection)
and configure the thread’s run loop if
you planned to use it. For information
on how to configure new threads, see
“Configuring Thread Attributes.”

As for what happens if you do:

[self performSelectorInBackground:@selector(method1) withObject:nil];
[self performSelectorInBackground:@selector(method2) withObject:nil];

...you will spawn two new threads, one of which starts executing at method1 and one of which starts executing at method2. The threads may execute concurrently (i.e. the second one will not wait for the first one to terminate before it starts executing).

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