Simple use and detailed description of IOS multithreading _ GCD

Source: Internet
Author: User

Simple use and detailed description of IOS multithreading _ GCD

// You can first look at the effect of this example to find a feeling, and then explain again

@ InterfaceyxpGCDVController ()

{

UIImageView * _ imageView;

}

@ End


@ Implementation yxpGCDVController


-(Void) viewDidLoad

{

[SuperviewDidLoad];

Self. title = @ "GCD ";

// Initialize an _ ImageView

_ ImageView = [[UIImageViewalloc] initWithFrame: CGRectMake (300,450,)];

_ ImageView. backgroundColor = [UIColorgrayColor];

_ ImageView. animationDuration = 3.0;

_ ImageView. animationRepeatCount = 0;

[Self. viewaddSubview: _ imageView];

// Asynchronous execution queue in the background

Dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {

NSString * url1 = @ "http://h.hiphotos.baidu.com/image/w%3D230/sign=b2d5c289123853438ccf8022a311b01f/91ef76c6a7efce1b1ae9f92fad51f3deb58f6510.jpg ";

NSString * url2 = @ "http://h.hiphotos.baidu.com/image/pic/item/d058ccbf6c81800aae834e8bb33533fa838b47d5.jpg ";

NSString * url3 = @ "http://d.hiphotos.baidu.com/image/pic/item/f2deb48f8c5494eec3ba65132ff5e0fe99257e1b.jpg ";

NSString * url4 = @ "http://g.hiphotos.baidu.com/image/pic/item/a6efce1b9d16fdfa81f4ace4b68f8c5494ee7b1b.jpg ";

NSString * url5 = @ "http://g.hiphotos.baidu.com/image/pic/item/d6ca7bcb0a46f21f70031fdbf4246b600c33ae07.jpg ";

NSArray * array = [[NSArrayalloc] initWithObjects: url1, url2, url3, url4, url5, nil];

NSMutableArray * imageArray = [[NSMutableArrayalloc] initWithCapacity: 20];

For (NSString * stringin array ){

// Download the image

NSLog (@ "execute image download function ");

NSData * data = [NSDatadataWithContentsOfURL: [NSURLURLWithString: string];

UIImage * image = [UIImageimageWithData: data];

[ImageArrayaddObject: image];

}

_ ImageView. animationImages = imageArray;

// Return to the main thread execution Method

Dispatch_async (dispatch_get_main_queue (), ^ {

[_ ImageView startAnimating];

});

});

}


@ End


// GCD usage

Dispatch_async (dispatch_queue_t queue, dispatch_block_t block );

Async indicates the Running Mode

Queue is the thread queue that you send the task.

Block indicates what you want to do.

// Thread running mode

Dispatch_async asynchronous execution

Synchronous execution of dispatch_sync

Dispatch_delay delayed execution

...

// Process the task object dispatch queue (thread queue)

1. dispatch_get_main_queue main thread Queue (UI thread Queue)

Ii. dispatch_get_global_queue: Parallel thread queue. The system creates three dispatch queue with different priorities. The execution sequence of the parallel queue is the same as that of the queue.

3. Serial queues are generally used for sequential synchronous access and sequential execution (in sequence determined by the Code). You can create any number of serial queues, and each serial queue is concurrent.

The serial queue is useful when you want to execute tasks in a specific order. The serial queue only executes one task at the same time. We can use serial queues instead of locks to protect shared data. Unlike locks, a serial queue ensures that tasks are executed in a predictable order.

The serial queue is created through dispatch_queue_create. You can use the dispatch_retain and dispatch_release functions to increase or decrease the reference count.



// Dispatch queue is called a thread queue

Dispatch_queue_create is used to create a user thread queue. You can create two queues: Serial and Concurrent Dispatch Queue.

1. Create a Serial Dispatch Queue.

Dispatch_queue_t serialQueue = dispatch_queue_create ("com. SerialQueue", NULL );

Multiple serial queues can be created and executed in parallel. You must not generate a large number of Serial Dispatch Queue at will.

2. Create A Concurrent Dispatch Queue

Dispatch_queue_t concurrentQueue = dispatch_queue_create ("com. ConcurrentQueue ",

DISPATCH_QUEUE_CONCURRENT );

Concurrent Dispatch Queue is not a problem because the threads used by Concurrent Dispatch Queue are efficiently managed by the system's XNU kernel and do not affect system performance.


// A thread that runs only once is generally used to write the singleton mode.

Static dispatch_once_t onceToken;

Dispatch_once (& onceToken, ^ {

});

EG:

StaticSubObject * subobject = nil;

+ (SubObject *) sharedRequest

{

Static dispatch_once_t onceToken;

Dispatch_once (& onceToken, ^ {

Subobject = [[SubObjectalloc] init];

});

Returnsubobject;

}


// The execution thread is delayed for 2 seconds.

Double delayInSeconds = 2.0;

Dispatch_time_t popTime = dispatch_time (DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC );

Dispatch_after (popTime, dispatch_get_main_queue (), ^ (void ){

// Code to be executed on the main queue after delay

});

// Thread Summary Notification. Wait until all the sub-threads (this name is not accurate, it is replaced first) are executed before the notify thread is executed.

Dispatch_group_t group = dispatch_group_create ();

Dispatch_group_async (group, dispatch_get_global_queue (0, 0), ^ {

// Thread 1 for Parallel Execution

For (int I = 0; I <10; I ++ ){

NSLog (@ "--------- % I", I );

}

});

Dispatch_group_async (group, dispatch_get_global_queue (0, 0), ^ {

// Thread 2 for Parallel Execution

For (int I = 10; I <20; I ++ ){

NSLog (@ "++ % I", I );

}

});

Dispatch_group_notify (group, dispatch_get_global_queue (0, 0), ^ {

// Summary Result

NSLog (@ "finished ");

});


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.