Objective-C、NSThread 分离与 PerformSelectorInBackground
这两者有什么区别?
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它们是相同的。这是官方文档 关于这个话题不得不说:
至于如果这样做会发生什么:
...您将生成两个新线程,其中一个在
method1
处开始执行,其中一个在method2
处开始执行。线程可以同时执行(即第二个线程在开始执行之前不会等待第一个线程终止)。They are identical. Here is the what the official documentation has to say on this topic:
As for what happens if you do:
...you will spawn two new threads, one of which starts executing at
method1
and one of which starts executing atmethod2
. The threads may execute concurrently (i.e. the second one will not wait for the first one to terminate before it starts executing).