Does performSelectorInBackground spawn new thread for each call?

只谈情不闲聊 提交于 2019-12-10 18:38:18

问题


Does performSelectorInBackground spawn a new thread for each call or does it share a thread (which is not main thread) for all calls (maybe queued)?


回答1:


A new thread is created with each call to -performSelectorInBackground:withObject:

From the Threading Programming Guide

Using NSObject to Spawn a Thread

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.”



来源:https://stackoverflow.com/questions/4688118/does-performselectorinbackground-spawn-new-thread-for-each-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!