GCD synchronous asynchronous serial parallel, Nsoperation, and Nsoperationqueue single-instance dispatch_once implementations

Source: Internet
Author: User

Ext.: http://www.tuicool.com/articles/NVVnMn

(1) synchronous asynchronous, serial parallel implemented by GCD.

--Sync Sync application scenario: User login, using blocking

--Serial Asynchronous Application scenario: Download time-consuming tasks

/** * Because it is asynchronous, the child thread is enabled, but because it is a serial queue, only 1 child threads (2) are required to execute sequentially in the child thread. Most commonly used. */-(void) gcddemo1{dispatch_queue_t Q1=dispatch_queue_create ("Com.hellocation.gcdDemo", dispatch_queue_serial);  for (int i=0; i<10; i++) { Dispatch_async (Q1, ^{  NSLog (@"%@", [Nsthread CurrentThread]); });}}/** * Because it is asynchronous, so the child thread, and because it is a parallel queue, so opened a lot of sub-threads, the specific few, no one knows, see luck. The number of threads is uncontrollable and wasteful. */-(void) gcddemo2{dispatch_queue_t Q2=dispatch_queue_create ("Com.hellocation.gcdDemo", dispatch_queue_concurrent); for (int i=0; i<10; i++) { Dispatch_async (Q2, ^{  NSLog (@"%@", [Nsthread CurrentThread]); });}}/** * Because it is synchronous, both the parallel queue and the serial queue are executed in the main thread */-(void) gcddemo3{dispatch_queue_t Q1=dispatch_queue_create ("Com.hellocation.gcdDemo", dispatch_queue_serial); for (int i=0; i<10; i++) { Dispatch_sync (Q1, ^{  NSLog (@"%@", [Nsthread CurrentThread]); });}}/** * Global queues are similar to parallel queues (global queues do not need to create a direct get, which leads to No name and is not conducive to subsequent debugging) */-(void) gcddemo5{dispatch_queue_t Q=dispatch_get_global_queue (Dispatch_queue_priority_default,0); for (int i=0; i<10; i++) { Dispatch_sync (q, ^{  NSLog (@"%@", [Nsthread CurrentThread]); });} for (int i=0; i<10; i++) { Dispatch_async (q, ^{  NSLog (@"%@", [Nsthread CurrentThread]); });}}/** * because it is the main thread, the asynchronous task also runs on the main thread (1). If it is a synchronous task, it is blocked because the main thread will always be running, so the post-meter task will never be executed. * Main use is to update the UI, update UI is implemented on the main thread */-(void) gcddemo6{ dispatch_queue_t q= Dispatch_get_main_queue ();  for (int i=0; i<10; i++) {  Dispatch_sync (q, ^{  Span class= "indent" > NSLog (@ "%@", [Nsthread CurrentThread]);  }); }//for (int i=0; i<10; i++) {//dispatch_async (q , ^{//nslog (@ "%@", [Nsthread CurrentThread]); //}); //}}              

(2) thread management implemented by Nsoperation and Nsoperationqueue

/** * 1, as long as the queue you create, add in the operation (here is the block operation), all on the child thread (2) * 2, as long as the main queue, the addition of operations, all on the main thread (1) * Two queues can not simultaneously grab a task operation */-(void) opdemo1{Nsoperationqueue *queue=[[nsoperationqueue Alloc]init];Nsblockoperation *b=[nsblockoperation blockoperationwithblock:^{  NSLog (@"%@", [Nsthread CurrentThread]);}];[Queue addoperation:b];[[Nsoperationqueue mainqueue]addoperation:b];}/** * Ibid. */-(void) opdemo2{Nsinvocationoperation *i=[[nsinvocationoperation alloc]initwithtarget:self Selector:@selector (HelloWorld) Object:nil];Nsoperationqueue *queue=[[nsoperationqueue Alloc]init];[Queue addoperation:i];[[Nsoperationqueue mainqueue]addoperation:i];} -(void) helloworld{NSLog (@"hello,world!");}/** * Dependencies: (1) The order of execution can be guaranteed, and the number of sub-threads to be opened is not too much, (2) can cross the queue, and serial is not to cross the queue, such as the last update UI becomes in the main queue. * This is the advantage of nsoperation (Nsblockoperation and Nsinvocationoperation) and Nsoperationqueue */-(void) opdemo3{Nsblockoperation *op1=[nsblockoperation blockoperationwithblock:^{ NSLog (@"Download Picture%@", [Nsthread CurrentThread]);}];Nsblockoperation *op2=[nsblockoperation blockoperationwithblock:^{ NSLog (@"Retouching picture%@", [Nsthread CurrentThread]);}];Nsblockoperation *op3=[nsblockoperation blockoperationwithblock:^{ NSLog (@"Save Picture%@", [Nsthread CurrentThread]);}];  Nsblockoperation *op4=[nsblockoperation blockoperationwithblock:^{ Span class= "indent" > NSLog (@ "Update UI%@", [Nsthread CurrentThread]); }];  [OP4 ADDDEPENDENCY:OP3];  [op3 ADDDEPENDENCY:OP2];  [OP2 ADDDEPENDENCY:OP1];  Nsoperationqueue *queue=[[nsoperationqueue Alloc]init];  //set the maximum number of threads open at the same time, which is nsoperationqueue-specific  [queue Setmaxconcurrentoperationcount:2];  [queue ADDOPERATION:OP1];  [queue addoperation:op2];  [queue addoperation:op3];  [[Nsoperationqueue MAINQUEUE]ADDOPERATION:OP4];}        

(3) Single example implementation (handwritten singleton request) Dispatch_once application, that is, the Allocwithzone method of overriding class

@implementation wpobject+ (Instancetype) Allocwithzone: (struct _nszone *) zone{  static wpobject *insta;   static dispatch_once_t Oncetoken; dispatch_once (&oncetoken, ^{ insta=[Super Allocwithzone:zone]; }); return Insta;} @end        

GCD synchronous asynchronous serial parallel, Nsoperation, and Nsoperationqueue single-instance dispatch_once implementations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.