"iOS Dev-91" GCD synchronous asynchronous serial parallel, Nsoperation, and Nsoperationqueue single-use dispatch_once implementations

Source: Internet
Author: User

(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, either a parallel queue or a serial queue, the */-(void) gcddemo3{dispatch_queue_t q1=dispatch_queue_create is executed in the main thread ("    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 results in no name and is not conducive to subsequent debugging) */-(void) gcddemo5{dispatch_queue_tQ=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, ^{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 that you create, add in the operation (here is the block operation), all on the child thread (2) * 2, as long as the main queue, add the operation, 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: @selec    Tor (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:^{NSLog (@ "Update UI%@", [Nsthread CurrentThread]);    }];    [OP4 ADDDEPENDENCY:OP3];    [Op3 ADDDEPENDENCY:OP2];    [Op2 ADDDEPENDENCY:OP1];    Nsoperationqueue *queue=[[nsoperationqueue Alloc]init];    Sets the maximum number of threads to open at the same time, which is unique to Nsoperationqueue [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

"iOS Dev-91" GCD synchronous asynchronous serial parallel, Nsoperation, and Nsoperationqueue single-use 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.