ios-Multithreading-GCD

Source: Internet
Author: User

I. Noun Explanation:

1. Processes and Threads

A process is an application that is running in the system. Each process is independent, and each process runs in a dedicated and protected memory space.

Thread refers to a process that wants to perform a task and must have a thread. Threads are the basic unit of a process, and all tasks of a process are performed in a thread.

2. Detailed description of multi-threading please see the blog of the Great God at the top of the text. http://www.cnblogs.com/wendingding/p/3805088.html

Two. Queues

/**/

Three. Tasks

/**/

Four.

    /** Get the main thread 1. All tasks that refresh the UI interface are performed on the main thread.     2. Put time-consuming tasks out of other threads and try not to process them in the main thread. */dispatch_queue_t Main_queue=Dispatch_get_main_queue (); NSLog (@"main_queue:\n%@", Main_queue); /** Self-created queue dispatch_queue_create parameter 1: The first parameter is an identifier. Used for debug flags the only queue that can be empty. Parameter 2: The second parameter is used to indicate whether the created queue is serial or parallel. An incoming dispatch_queue_serial or null means that a serial queue is created. The incoming dispatch_queue_concurrent represents the parallel queue that was created. (serial--> SERIAL Continuous/concurrent--> CONCURRENT, concurrent, consistent)*/        //Create a serial queuedispatch_queue_t Serialqueue =dispatch_queue_create (nil, NULL); NSLog (@"serialqueue:\n%@", Serialqueue); //Create a parallel queue: this should be the only parallel queue, as long as parallel tasks are generally added to this queuedispatch_queue_t Concurrentqueue =dispatch_queue_create (nil, dispatch_queue_concurrent); NSLog (@"concurrentqueue:\n%@", Concurrentqueue); //Create a task    /** Sync Task (Sync) 1. No additional threading. */Dispatch_sync (Serialqueue,^{                for(inti =0; I <10000; i + +) {NSLog (@"Sync Task: \n%@", [Nsthread CurrentThread]);        }    }); /** Synchronization Task (Async) 1. Threads will be opened separately. */Dispatch_async (Serialqueue,^{NSLog (@"Asynchronous task:%@", [Nsthread CurrentThread]); });

Five. Example Introduction

NSLog (@"before ==>%@", [Nsthread CurrentThread]); Dispatch_sync (Dispatch_get_main_queue (),^{NSLog (@"sync==>%@", [Nsthread CurrentThread]);        }); NSLog (@"after ==>%@", [Nsthread CurrentThread]); /** Explanation 1. will only print the first sentence: before ==> <nsthread:0x7fe66b700610>{number = 1, name = main}, and then the main thread is stuck, you can put a button on the interface, you will find that the point is not     Lipped 2. After the first sentence is printed, dispatch_sync (because it is a synchronization task that blocks the current thread) blocks the current main thread and then puts the tasks in the block into Main_queue, and the tasks in the main_queue are taken out to be executed in the main thread.     But the main course of breeding pigeons has been blocked, so the block Breeder's mission can not be completed, it is not completed, Dispatch_sync will always block the main thread. Causes the main thread to remain stuck. This is the deadlock phenomenon. */
dispatch_queue_t queue = Dispatch_queue_create ("Myqueue", dispatch_queue_serial); NSLog (@"output 1. Before ==>%@", [Nsthread CurrentThread]); Dispatch_async (Queue,^{NSLog (@"==>%@ before output 2.sync", [Nsthread CurrentThread]); Dispatch_sync (Queue,^{NSLog (@"Output 3.sync==>%@", [Nsthread CurrentThread]);        }); NSLog (@"output 4.sync after ==>%@", [Nsthread CurrentThread]);        }); NSLog (@"output 5. After ==>%@", [Nsthread CurrentThread]); /** Explanation 1. The current thread is the default main thread 2. The output is 1, output 5 and Output 2 are output. Output 3 and output 4 are not executed.        3. Analyze in order of execution.        (1) The queue that we created is a serial queue (dispatch_queue_serial). The serial queue is characterized by the holding of a task that takes out one execution. The current task is not completed and the next task is not executed.        (2) Print out the output 1.        (3) An asynchronous task (async) is turned on in the queue. The asynchronous task is characterized by that the current thread is not blocked. So there are two threads, one is the main thread that executes output 5. The other is to execute output 2 in the newly opened queue thread. (4) In a queue thread that has been opened, a synchronous task (sync) is performed, and the synchronization task is characterized by the execution of a task that blocks the current thread. The current thread is a queue and has been blocked. It is also required to perform the next task. It creates a deadlock phenomenon. So sync     Where the thread is stuck, output 3 and output 4 will naturally not print. */

Six. Queue groups can add many queues to a group, and the benefit is that when all the tasks in the group are executed, the queue group notifies us through a method.

    //1. Create a queue groupdispatch_group_t Group =dispatch_group_create (); //2. Creating a queue Dispatch_get_global_queue gets a global queue, which we'll take for a few global threads that the system opens for us. We use priority to prioritize the queue, and flag as the reserved field (typically 0). dispatch_queue_t Queue= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //3. Use the methods in the queue multiple times to perform tasks, only asynchronous Tasks//3.1 performing three cyclesDispatch_group_async (group, queue, ^{                for(inti =0; I <3; i + +) {NSLog (@"group-01-%@", [Nsthread CurrentThread]);        }    }); //3.2 Primary Queue performs 8 cyclesDispatch_group_async (Group, Dispatch_get_main_queue (), ^{                for(inti =0; I <8; i + +) {NSLog (@"group-02-%@", [Nsthread CurrentThread]);        }    }); //3.3 performing 5 cyclesDispatch_group_async (group, queue, ^{                 for(inti =0; I <5; i + +) {NSLog (@"group-03-%@", [Nsthread CurrentThread]);        }    }); Dispatch_group_notify (Group, Dispatch_get_main_queue (),^{NSLog (@"Finish-%@", [Nsthread CurrentThread]); });

ios-Multithreading-GCD

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.