[IOS development-91] synchronous asynchronous serial parallel, NSOperation, and NSOperationQueue of GCD use dispatch_once to implement Singleton,-91dispatch_once

Source: Internet
Author: User

[IOS development-91] synchronous asynchronous serial parallel, NSOperation, and NSOperationQueue of GCD use dispatch_once to implement Singleton,-91dispatch_once

(1) Synchronous asynchronous and serial parallel implemented by GCD.

-- Synchronous sync Application Scenario: User Login, blocking

-- Serial Asynchronous Application Scenario: Download and other time-consuming tasks

/*** Subthreads are activated because they are asynchronous, but because they are serial queues, only one subthread (2) needs to be activated and executed sequentially in the subthread. 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]);}) ;}/ *** subthreads are activated because they are asynchronous, and because they are parallel queues, many subthreads are activated, the details are as follows: No one knows. Check for luck. The number of threads cannot be controlled and is wasted. */-(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]) ;}} /*** the global queue is similar to the parallel Queue (the Global queue does not need to be created directly with get, so it has no name, which 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 Is the main thread, so the asynchronous task will also run on the main thread (1 ). If it is a synchronization task, it is blocked. Because the main thread is always running, the subsequent task will never be executed. * It is mainly used to update the UI. All updated UIS are 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) NSOperation and NSOperationQueue thread management

/*** 1. As long as it is a self-created queue, the added operations (block operations here) are all on the sub-thread (2) * 2. As long as the added operations are in the main queue column, they are all in the main thread (1) * two queues cannot simultaneously grab one task operation */-(void) opDemo1 {NSOperationQueue * queue = [[NSOperationQueue alloc] init]; NSBlockOperation * B = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "% @", [NSThread currentThread]);}]; [queue addOperation: B]; [[NSOperationQueue mainQueue] addOperation: B];}/*** same as above */-(void) opDemo2 {NSInvocationOperation * I = [[NSInvocationOperation alloc] handler: self selector: @ selector (helloWorld) object: nil]; NSOperationQueue * queue = [[NSOperationQueue alloc] init]; [queue addOperation: I]; [[NSOperationQueue mainQueue] addOperation: I];}-(void) helloWorld {NSLog (@ "hello, world! ");}/*** Dependency: (1) the execution sequence can be ensured, so that there are not too many sub-threads opened; (2) Cross-queue, serial mode cannot be used across queues. For example, the last updated UI is changed to the main queue column. * This is the advantage of NSOperation (NSBlockOperation and NSInvocationOperation) and dispatch */-(void) opDemo3 {NSBlockOperation * op1 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Download image % @", [NSThread currentThread]);}]; NSBlockOperation * op2 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "modify image % @", [NSThread currentThread]);}]; NSBlockOperation * op3 = [NSBlockOperation blockOperationWithBlock: ^ {NSLog (@ "Save image % @", [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 enabled 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) Implementation of Singleton (handwritten Singleton Requirements) dispatch_once application, that is, rewrite the allocWithZone method of the 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

Related Article

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.