Three multithreading techniques for 1.iOS 1.NSThread each Nsthread object corresponds to one thread, with a light level (true multi-threaded) 2. The following two points are Apple's "concurrency" technology specifically developed to allow programmers to stop worrying about thread-specific usage issues ØNSOP Eration/nsoperationqueue Object-oriented threading technology Øgcd--grand Central Dispatch (distributed) is a C-based framework that can take full advantage of multicore and is the recommended multithreaded technology for Apple
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 multithreading techniques nsthread:– Pros: Nsthread is lighter than the other two lightweight, easy to use – Cons: You need to manage your thread's life cycle, thread synchronization, locking, sleep, and wake up. Thread synchronization has a certain overhead for 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 an 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's 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
Member Method Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (Bigdemo) Object:nil]; Start the start thread [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 10 11}
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 operations queue, the operation starts with 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
Two ways [Nsopeationqueue mainqueue] addoperation ^{}; [[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
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
From: http://www.cnblogs.com/qingche/p/3496190.html
Comparison and realization of three kinds of multithreading technology in IOS-NSTHREAD/NSOPERATION/GCD--turn