I go, good egg hurts, just write good blog because the hand in the touch panel on the right row, write all lost, also have to re-write, blog Park There is no solution for this situation? I don't want to write.
One, there are four ways to implement multi-threading in iOS
(1) Nsthread traps are very many, defective, but OC, and occasionally use
(2) gcd in Apple in IOS4, can make full use of the device multi-core, and do not consider the thread, performance than nsthread much better
GCD is more deeply researched, so it is often asked in the interview.
(3) Nsoperation encapsulated a lot of use of the function, in some cases, GCD implementation is more difficult to achieve with nsoperation is very simple, Apple recommended everyone use Nsoperation
Because the Nsoperation package is very good, so it is very simple to use, no technical difficulties, in the interview did not ask
Two. How to use Nsthread
Nsthread is easy to use and more flexible.
(1) General method: CurrentThread
Nsthread *thread = [Nsthread CurrentThread];
The current thread, which can be used in all multithreading techniques (referred to as nsthread\gcd\nsopertion) to get the current thread
(2) The first method, the simplest one
/** instantiate Initwithtarget method without parameters */
// 1. Instantiating a thread object, run is a time-consuming task that works on other threads without affecting the main thread! Object: nil]; // 2. Start the thread and call the Run method immediately [thread start];
Instantiate a thread object and place time-consuming tasks on other threads
Must start before the Run method is executed
Initwithtarget method for instantiating/** with parameters */
Object:@ "hehe"]; @" Thread Initwithtarget ";
Note that with the parameter @selector (run:) plus a colon
You can set the Name property of the Thread object
(3) The second method
/** Detach method without parameters */
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];
-(void) run
A new thread is started immediately, running the Run method
Detach method for/** with parameters */
// object is the first argument to call a method [Nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "Hello"];
-(void) Run: (ID) obj
Note that with the parameter @selector (run:) plus a colon
(4) The third method