Three multithreading techniques for 1.iOS
1.NSThread each Nsthread object corresponds to one thread, the magnitude is lighter (true multithreading)
2. The following two points are Apple's specially developed "concurrency" technology, which allows programmers to stop worrying about thread-specific usage issues Nsoperation/nsoperationqueue object-oriented threading technology Gcd--grand Central Dispatch (distributed) is based on the C language framework, can make full use of multicore, Apple is recommended to use multithreading technology. The above three kinds of programming from top to bottom, the level of abstraction from low to high, the higher the abstraction of the more simple to use, is also the most recommended by Apple, many of the framework technology in the project using different multithreading technology. 2. Comparison of three kinds of multithreading techniques
? Nsthread:
– Pros: Nsthread than the other two lightweight, simple to use – Cons: You need to manage your thread's lifecycle, thread synchronization, locking, sleep, and wake up. Does thread synchronization have a certain overhead in locking data? nsoperation:– do not need to care about thread management, data synchronization, you can focus on the actions you need to perform –nsoperation is object-oriented? Gcd:–grand Central Dispatch is a multi-core programming solution developed by Apple. Ios4.0+ can be used, is an alternative to nsthread, Nsoperation's efficient and powerful technology –GCD is based on the C language of 3. Three kinds of multithreading technology implementation 3.1. Nsthread multithreading Technology, the 1> class method opens the background thread directly and executes the selector method
Detachnewthreadselector
1 //Create a new thread, call @selector Method 2 3 [Nsthread detachnewthreadselector: @selector (Bigdemo) totarget:self Withobject:nil];
2> member method, after instantiating the thread object, you need to execute the Selector method using start
Initwithtarget
1//Member Method 2 nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (Bigdemo) object:nil];3 4 //Start the start thread 5 [Thread start];
For the simple use of nsthread, can be replaced with NSObject Performselectorinbackground
1 //Performselectorinbackground is to put Bigdemo's task in a background thread to execute 2 3 [self performselectorinbackground: @selector ( Bigdemo) Withobject:nil];
At the same time, in the method called by Nsthread, we also use Autoreleasepool for memory management, otherwise it is prone to memory leaks.
1// auto-Release Pool 2 3 //Responsible for memory management on other threads, when using Nsthread or NSObject thread methods, be sure to use automatic release Pool 4 5 //otherwise prone to memory leaks. 6 7 @autoreleasepool {8 9 }
3.2 Nsoperation, Object-oriented multithreading technology
1> Steps to use:
1) instantiation of the operation
1//instantiation of the Operation queue 2 _queue = [[Nsoperationqueue alloc] init];
A) nsinvocationoperation
1 nsinvocationoperation *op1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (opaction) object: NIL];2 3 //If Start is used, the operation will start in the current thread 4 // [OP1 start];5 6 //1. Once the action is added to the action queue, the operation starts 7 [_queue ADDOPERATION:OP1];
b) nsblockoperation
1 #pragma mark imitate download network Image 2-(ibaction) OPERATIONDEMO3: (ID) Sender 3 {4 //1. Download 5 nsblockoperation *op1 = [Nsblocko Peration blockoperationwithblock:^{6 NSLog (@ "Download%@", [Nsthread CurrentThread]); 7 }]; 8 //2. Filter 9 NSBL Ockoperation *OP2 = [nsblockoperation blockoperationwithblock:^{10 NSLog (@ "Filter%@", [Nsthread CurrentThread]); 11 }]; 3. Display nsblockoperation *op3 = [nsblockoperation blockoperationwithblock:^{14 NSLog (@ "Update UI%@", [ Nsthread CurrentThread]); }];16 //Add the dependency between operations, so-called "dependency" relationship, is to wait for the previous task to complete, the latter task to start Dependencies can be implemented across thread queues //hints: When specifying dependencies, be careful not to loop dependencies or not work. [Op2 adddependency:op1];21 [op3 adddependency:op2];22// [OP1 adddependency:op3];23 24 [_queue addoperation:op1];25 [_queue addoperation:op2];26 [[Nsoperationqueue mainqueue] addoperation:op3];27}
2) Add operation to queue nsoperationqueue to start multithreaded execution
1 [_queue addoperation:op1];2 [_queue ADDOPERATION:OP2];
2> updating UI using the main thread queue
1 //Two Way 2 3 [nsopeationqueue mainqueue] addoperation ^{4 5 6 7}; 8 9 [[ Nsoperationqueue Mainqueue] ADDOPERATION:OP3];
Setmaxconcurrentoperationcount of the 3> operation queue
You can set the number of concurrent threads at the same time!
1//control simultaneous maximum number of concurrent threads 2 [_queue setmaxconcurrentoperationcount:2];
Hint: This feature only has nsoperation!
4> use Adddependency to set the order in which tasks are executed, and to specify dependencies across operational queues
1 //Add the dependencies between the actions, the so-called "dependency" relationship, is to wait for the previous task to complete after the next task to start 2 3 //dependencies can be implemented across thread Queues 4 5 //hint: When specifying dependencies, be careful not to loop dependencies or not work. 6 [Op2 adddependency:op1];7 [op3 adddependency:op2];8 [OP1 ADDDEPENDENCY:OP3];
Tip: When specifying dependencies, be careful not to loop dependencies or not work.
3.3. Gcd,c language
GCD is to use multithreading technology on multicore.
1> to use GCD, all the methods are dispatch beginning.
2> noun explanation
Global globally
Queue queues
Async Async
Sync synchronization
3> to perform an asynchronous task, execute it in the global queue
Dispatch_async asynchronous execution can't control the sequencing
4> about the GCD queue
Global Queue Dispatch_get_global_queue
Parameter: Priority Dispatch_queue_priority_default
is always 0
1 dispatch_queue_t queue = dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
can be synchronously asynchronous
Serial queue
1 dispatch_queue_t queue = dispatch_queue_create ("Myqueue", dispatch_queue_serial);
is created and cannot be obtained directly
can only synchronize
Main queue Dispatch_get_main_queue
1 Dispatch_async (Dispatch_get_main_queue (), ^{2 NSLog (@ "Main->%@", [Nsthread CurrentThread]); 3 });
Only on the same foot
5> asynchronous and synchronous are independent of the method name and are related to the queue where they are running!
Synchronization is primarily used to control the order in which methods are called
Three types of multithreading technology for iOS NSTHREAD/NSOPERATION/GCD