Multithreading GCD Usage

Source: Internet
Author: User
Tags button type gcd

/*------------------------------GCD use 1. Queue and Task------------------------------------------*/

Focus: 1. " Serial queue "? "Concurrent queues"? 2.block?

{

1.GCD (Grand Central Dispatch)----' Awesome hub scheduler '!

C Language Framework/automatic management of Thread life cycle (Create/release)

The purpose of introducing GCD: replacing nsthread!

A solution for the "parallel" operation of "multicore"!

Advantages:

<1> GCD can automatically take advantage of more CPU cores (dual-core/four-core)!

<2> GCD automatically manages the life cycle of the thread.

The two core concepts in 2.GCD:

"Tasks":

What you want to do/what to do with it.

The tasks in GCD are defined in the block.

void (^myblock) () = ^{

What you want to do/the task

}

"Queue":

Used to ' Store ' tasks!

Queue! = thread!

The tasks that are stored in the queue are finally executed by the thread!

Queue principle: FIFO, LIFO (Fifo/first in First out)!

Type of queue:

<1> ' Serial ' Queues: (Serial Dispatch queue)

Store tasks in order! (one task is completed and the next task is executed)!

Create a serial queue

dispatch_queue_t serialqueue = dispatch_queue_create ("Serial", dispatch_queue_serial);

<2> ' concurrent ' queue: (Concurrent Dispatch queue)

Store tasks that you want to perform concurrently (concurrently)!

Create a concurrent queue

dispatch_queue_t concurrentqueue = dispatch_queue_create ("concurrent", dispatch_queue_concurrent);

Note the two very common special queues:

<1> Home column://UI actions are placed in the main queue to execute!

The queue associated with the mainline threads!

The main queue is a special serial queue that comes with GCD!

The tasks in the main queue will be executed in the main thread!

Get the Home row

dispatch_queue_t mainqueue = Dispatch_get_main_queue ();

<2> Global concurrency Queue://In general, concurrent tasks can be placed in global concurrent queues!

Get global concurrency Queue

dispatch_queue_t globalqueue = dispatch_get_global_queue (0, 0);

}

/*-----------------------------------GCD use 2. Perform the task-------------------------------------*/

Focus: 1. " Sync "Function!" Async "Function! 2. Easy to confuse four concepts: ' serial ', ' Concurrency ', ' sync ', ' async ' difference?

{

Question: Are tasks in the serial queue necessarily executed sequentially? must the tasks in the concurrent queue execute at the same time?

There are two functions used to perform the task in GCD:

' Sync ' to perform tasks:

Dispatch_sync (< #dispatch_queue_t queue#>, <#^ (void) block#>)

' Async ' To perform the task:

Dispatch_async (dispatch_queue_t queue, <#^ (void) block#>)

< #dispatch_queue_t queue#>: queue

<#^ (void) Block#>: Task

The difference between "synchronous" and "asynchronous":

"Synchronization": The task can only be performed in the ' current ' thread and does not have the ability to open a new thread.

"Async": You can perform tasks in a ' new ' thread, with the ability to open new threads.

There are two steps to using the GCD:

<1> add tasks to the queue;

<2> perform tasks in a synchronous or asynchronous manner.

Note: Four easy-to-confuse terms:

' Serial ', ' concurrency ', ' synchronous ', ' async '.

}

/*-------------------------------GCD use 3. The execution effect of various queues---------------------------------*/

Focus: 1. Two common combinations!

{

Common combinations

1> Dispatch_async + Global Concurrent Queue (multiple threads can be opened)

2> Dispatch_async + self-created serial queue (opens a thread)

Only the ' asynchronous ' execution of the concurrent queue can open multiple threads.

Attention:

Synchronizing the tasks in the main queue in the primary thread will cause the ' main thread ' and ' home row ' to wait for each other and get stuck in the main thread!

}

/*-------------------------------GCD use 4. Inter-thread communication---------------------------------------*/

Focus: 1. Go back to the main thread from the child threads! 2. Two points of attention.

{

1. (Sub-thread download (time consuming operation), main thread refresh UI):

Dispatch_async (dispatch_get_global_queue (0, 0), ^{

Perform time-consuming asynchronous operations ...

Dispatch_async (Dispatch_get_main_queue (), ^{

Go back to the main thread and perform a UI refresh operation

});

});

2. Note:

<1> need to set the image of the button, it is recommended to change the button type to custom to ensure that the setting is successful

<2> attribute names cannot start with new

}

/*-------------------------------GCD use 5. Delay Execution-----------------------------------------*/

Focus: 1.iOS common two kinds of delay execution mode

{

Deferred execution mode in iOS:

After you have customized the delay task, the current thread is not blocked.

<1> Call the NSObject method:

[Self performselector: @selector (Run) Withobject:nil afterdelay:2.0];

Call the self's Run method after 2 seconds

The <2> GCD function implements delay execution:

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

After 2 seconds, execute the code here ... On which thread to execute, with respect to the queue type

});

Do not use sleep, which blocks the current thread.

}

/*-------------------------------GCD use 6. Queue Group------------------------------------------*/

Focus: 1. Understand how queue groups are used.

{

First of all: two time-consuming operations are performed asynchronously, respectively;

Second: After two time-consuming operations are completed, then return to the main thread to perform the operation.

Use queue groups (dispatch_group_t) to quickly and efficiently implement the above requirements.

dispatch_group_t group = Dispatch_group_create (); Queue Group

dispatch_queue_t queue = dispatch_get_global_queue (0, 0); Global concurrent queues

Dispatch_group_async (group, queue, ^{//asynchronous perform operation 1

LongTime1

});

Dispatch_group_async (group, queue, ^{//asynchronous perform operation 2

LongTime2

});

Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{//Refresh data on the main thread

Reload Data

});

}

Multithreading GCD Usage

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.