Introduction to iosGCD 2

Source: Internet
Author: User

Introduction to iosGCD 2

Today, the wind blows me away, but I still come back... La la, let's make a short speech. The following provides an in-depth understanding of GCD. You can copy it to your own Xcode and run it. Then, let's take a closer look at these introductions and repeat them several more times. In fact, it is very simple. A concurrent serial queue... Just like we walk, three people walk in a row of concurrency, and three people clap the teams one by one, that is, serial queue .. Haha, isn't it interesting?

 

# Import "ViewController. h"

 

@ Interface ViewController ()

 

@ End

 

@ Implementation ViewController

 

-(Void) viewDidLoad {

[Super viewDidLoad];

 

// ------------------ GCD ---------------------

// 1. What is GCD?

// Grand Central Dispatch (large Central scheduling), pure C language, provides many powerful functions

 

// 2. GCD advantages

// 2.1 GCD is a solution proposed by Apple for multi-core parallel operations

// 2.2 GCD automatically utilizes more CPU cores (dual-core and quad-core)

// 2.3 GCD automatically manages the thread lifecycle (including creating threads, scheduling tasks, and destroying threads)

// 2.4 The programmer only needs to tell GCD what task to execute and does not need to write any thread Management Code

 

// ---------------- Task and queue -------------------------

// Two core concepts in GCD

// 1. What operations does the task perform?

// 2. queue: used to store tasks

 

// Use GCD in two steps

// 1. Custom queue

// 2. Determine what you want to do

// Add the task to the queue. GCD automatically extracts the task from the queue and puts it in the corresponding thread for execution. The task removal follows the FIFO principle of the queue: first-in-first-out, and then-out.

 

 

// ------------ Execute the task ----------------

// 1. There are two functions in GCD used to execute the task.

// Description: Submit the parameter (task) on the right to the parameter (Queue) on the left for execution.

// (1) execute the task dispatch_sync (dispatch_queue_t queue, <# ^ (void) block #>) in synchronous mode)

// (2) execute the task dispatch_async (dispatch_queue_t queue, <# ^ (void) block #> in asynchronous mode)

// 2. Differences between synchronous and asynchronous

// Synchronization: executed in the current thread

// Asynchronous: executed in another thread

 

// ------------- Queue -----------------------

// 1. Queue type

// Concurrent Dispatch Queue allows Concurrent execution of multiple tasks (enable multiple threads to execute tasks at the same time). The Concurrent function is only implemented by the asynchronous function (dispatch_async) valid only

// 1.2 The Serial Queue (Serial Dispatch Queue) allows the task to be executed one by one

 

 

// Synchronous, asynchronous, concurrent, serial

// Synchronization and Asynchronization determine whether to enable a new thread

// Synchronization: The task is executed in the current thread and cannot start a new thread.

// Asynchronous: execute tasks in the Sind thread and enable the new thread.

// Concurrency and serial determine the task execution mode

// Concurrency: Concurrent execution of multiple tasks

// Serial: After a task is executed, execute the next task.

 

// -------------------- Serial queue -------------------

// There are two methods to obtain the serial queue in GCD.

// 1. dispatch_queue_create (<# const char * label #>, <# dispatch_queue_attr_t attr #>) // queue name, queue attribute, generally NULL

// 2. dispatch_queue_t queue = dispatch_get_main_queue (), which is a special queue provided by GCD. All tasks in the main queue column are executed in the main thread.

 

// ----------------- Concurrent queue -------------------

// By default, GCD provides a global concurrent queue for the entire application. You do not need to manually create the queue.

// Dispatch_queue_t queue = dispatch_get_global_queue (<# long identifier #>, <# unsigned long flags #>) the first parameter is the priority, and the second parameter is the retention parameter, which will be used later, pass 0.

 

 

// ------------ Summary --------------------

// Synchronization functions do not have the ability to enable threads, and no thread is enabled in any queue.

// Asynchronous functions can enable threads. It is determined by the queue to enable several threads (only one new thread is enabled for the serial queue and multiple threads are enabled for the concurrent Queue)

 

 

// ----------- Supplement --------------------

// All functions with the words "create \ copy \ new \ retain" in their names must be release when this data is not required.

// The data type of GCD does not need to be release in the ARC environment.

// The data type of CF (core Foundation) still needs to be release in the ARC environment.

 

 

}

 

 

 

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

{

 

 

// [Self test1];

// [Self test2];

// [Self test3];

[Self test4];

 

}

 

 

// Add tasks to the serial queue of the synchronization function, and no new threads will be opened.

-(Void) test1

{

 

// 1. Create the Data Type of the serial queue _ t _ ref C Language

// Parameter 1: queue name

// Parameter 2: the queue attribute is generally NULL, indicating the creation of a serial queue,

// Specify DISPATCH_QUEUE_SERIAL in parameter 2 to create a serial queue.

// DISPATCH_QUEUE_CONCURRENT

Dispatch_queue_t queue = dispatch_queue_create ("Serial Queue", NULL );

 

 

 

Dispatch_sync (queue, ^ {

NSLog (@ "Download Task 1, curThread = % @", [NSThread currentThread]);

[NSThread sleepForTimeInterval: 2];

}); // The execution will not be received until the task is completed.

 

 

NSLog (@" 111 ");

 

Dispatch_sync (queue, ^ {

NSLog (@ "Download Task 2, curThread = % @", [NSThread currentThread]);

});

 

 

NSLog (@" 222 ");

 

 

Dispatch_sync (queue, ^ {

NSLog (@ "Download image Task 3, currThread = % @", [NSThread currentThread]);

});

 

 

NSLog (@" 333 ");

 

 

 

}

// Add tasks to the parallel queue of the synchronization function. New threads are not opened and the concurrent function is invalid.

-(Void) test2

{

// 1. Get the parallel queue. In iOS, the system has prepared a global queue for each application for one day. The Global queue is a parallel queue.

// Parameter 1: queue priority. The default priority is generally used.

// Parameter 2: Reserved parameter. Enter 0.

 

Dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 );

 

 

 

Dispatch_sync (queue, ^ {

NSLog (@ "Download image Task 1, curThread = % @", [NSThread currentThread]);

[NSThread sleepForTimeInterval: 2];

});

NSLog (@" 111 ");

 

Dispatch_sync (queue, ^ {

NSLog (@ "Download image Task 2, curThread = % @", [NSThread currentThread]);

});

NSLog (@" 222 ");

Dispatch_sync (queue, ^ {

NSLog (@ "Download image Task 3, curThread = % @", [NSThread currentThread]);

 

});

 

NSLog (@" 333 ");

 

}

 

 

/**

* Adding a task to the serial queue of an asynchronous function opens up a new thread, but only opens up one thread.

*/

-(Void) test3

{

// 1. Create a serial queue

Dispatch_queue_t queue = dispatch_queue_create ("Serial Queue", DISPATCH_QUEUE_SERIAL );

 

// 2. Add tasks to the serial queue using asynchronous Functions

Dispatch_async (queue, ^ {

NSLog (@ "Download image Task 1, curThread = % @",

[NSThread currentThread]);

[NSThread sleepForTimeInterval: 2];

});

 

NSLog (@" 1111 ");

 

Dispatch_async (queue, ^ {

NSLog (@ "Download image Task 2, curThread = % @",

[NSThread currentThread]);

});

 

NSLog (@" 2222 ");

 

Dispatch_async (queue, ^ {

NSLog (@ "Download image Task 3, curThread = % @",

[NSThread currentThread]);

[NSThread sleepForTimeInterval: 2];

});

 

NSLog (@" 333 ");

}

 

/**

* Adding tasks to parallel queues in asynchronous functions may open up multiple new threads

*/

-(Void) test4

{

// 1. Create a parallel queue

Dispatch_queue_t queue = dispatch_queue_create ("Concurrent Queue", DISPATCH_QUEUE_CONCURRENT );

 

// 2. Add tasks to the parallel queue using asynchronous Functions

Dispatch_async (queue, ^ {

[NSThread sleepForTimeInterval: 2];

NSLog (@ "Download image Task 1, curThread = % @",

[NSThread currentThread]);

});

 

NSLog (@" 1111 ");

 

Dispatch_async (queue, ^ {

NSLog (@ "Download image Task 2, curThread = % @",

[NSThread currentThread]);

});

 

NSLog (@" 2222 ");

 

Dispatch_async (queue, ^ {

NSLog (@ "Download image Task 3, curThread = % @",

[NSThread currentThread]);

// [NSThread sleepForTimeInterval: 2];

});

 

NSLog (@" 333 ");

}

 

 

@ 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.