Understanding of IOS GCD (i)

Source: Internet
Author: User

    • Basic concepts

Understanding multithreading before, first of all, we have to explain the operating system on the multi-threading terminology, students who have learned the operating system can skip.

    1. Process: A program with a certain set of independent functions for a single run activity of a data collection. Can be understood as a running app.
    2. Threads: the smallest unit that a program can perform operations on, and a process may contain multiple threads.
    3. Synchronization: The current thread can only be executed sequentially, without opening a new thread and returning after it has completed its scheduled task.
    4. Async: Multiple new threads can be executed on the current thread, the order of execution is not guaranteed, it will be returned immediately, the scheduled task will complete but will not wait for it to complete. Therefore, an asynchronous function does not block the current thread from executing the next function.
    5. Parallelism: For multicore CPUs, multiple tasks can be performed at the same time. For a single-core CPU, it is in a very short period of time that the fragment polls execution, which makes people feel that it is executing simultaneously.
    6. Serial: Thread execution can be executed sequentially in order.
    7. Queues: GCD provides a dispatch queues code block that manages the tasks you provide to GCD and performs these tasks in a FIFO order. This guarantees that the first task to be added to the queue will be the first task in the queue, and the second task to be added will start the second one, so that the end of the queue. The queues are divided into serial and parallel queues, and the tasks in the serial queue (Serial queus) serial queue execute one at a time, and each task starts only when the previous task completes. And, you don't know the length of time between the end of a Block and the next start. Concurrent queues (Concurrent Queus): Tasks in a concurrent queue are guaranteed to be executed in the order in which they are added, and the tasks may be completed in any order, and you will not know when to start running the next task, or how many blocks are running at any one time.
    • Characteristics of queues and tasks

The queue is scheduled in first-in, in-out manner, and when it is time to execute a task, it is taken out of the queue and given to a thread to execute.

    1. Serial queue: Tasks are dispatched in the order they are added, the current task is not completed, and the next task is not scheduled.
    2. Parallel queue: As long as there are idle threads, the queue will dispatch the current task, to the thread to execute, do not need to consider the previous task is executed, as long as the thread can be exploited, the queue will dispatch the task.
    3. Main queue: is a serial queue that is used primarily for UI update-related tasks.
    4. Global queue: A world queue is a parallel queue.
    5. Synchronization task: Does not open a new thread, the task is executed one after the other.
    6. Asynchronous task: A new thread is used, and the task can execute concurrently.
    • Task and queue combinations
    1. Serial Queue synchronization task: Combination features serial queue sequence execution, synchronization task does not open new thread, so it is one-by-one.
dispatch_queue_t Serialqueue = Dispatch_queue_create ("Com.gcd.demo", dispatch_queue_serial); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:3]; NSLog (@"Task 1");        }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:1]; NSLog (@"Task 2");        }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:2]; NSLog (@"Task 3");        }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:1]; NSLog (@"Task 4"); });

Print as follows:

2016-08-04 15:42:53.334 gcdtestdemo[6858:1582521] Task 1

2016-08-04 15:42:54.342 gcdtestdemo[6858:1582521] Task 2

2016-08-04 15:42:56.348 gcdtestdemo[6858:1582521] Task 3

2016-08-04 15:42:57.353 gcdtestdemo[6858:1582521] Task 4

2. Serial Queue asynchronous task: Serial queue The current task does not end and the next schedule is not scheduled. An asynchronous task One-by-one execution on another thread.

Execution results are as follows

  

3. Parallel Queue Synchronization task: Parallel queue does not wait for the current task, as long as the thread, the next task will be dispatched, the synchronization task will not open new threads, so the combination is still one-by-one execution.

  

    0), ^{        [self testserialqueue];        NSLog (@ "%@", [Nsthread CurrentThread]);    });

dispatch_queue_t Serialqueue = Dispatch_queue_create ("Com.gcd.demo", dispatch_queue_concurrent); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:3]; NSLog (@"Task 1%@", [Nsthread CurrentThread]);        }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:1]; NSLog (@"Task 2%@", [Nsthread CurrentThread]);        }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:2]; NSLog (@"Task 3%@", [Nsthread CurrentThread]);        }); Dispatch_sync (Serialqueue,^{[Nsthread sleepfortimeinterval:3]; NSLog (@"Task 4%@", [Nsthread CurrentThread]); });

Execution Result:

2016-08-04 18:03:52.823 gcdtestdemo[7064:1618448] Task 1 <nsthread:0x14d47590>{number = 3, name = (null)}

2016-08-04 18:03:53.827 gcdtestdemo[7064:1618448] Task 2 <nsthread:0x14d47590>{number = 3, name = (null)}

2016-08-04 18:03:55.832 gcdtestdemo[7064:1618448] Task 3 <nsthread:0x14d47590>{number = 3, name = (null)}

2016-08-04 18:03:58.838 gcdtestdemo[7064:1618448] Task 4 <nsthread:0x14d47590>{number = 3, name = (null)}

2016-08-04 18:03:58.838 gcdtestdemo[7064:1618448] <nsthread:0x14d47590>{number = 3, name = (NULL)}

4. Parallel Queue Asynchronous task: The next one describes the characteristics of the parallel queue, and asynchronous execution is the task can open a new thread, so this combination can achieve the concurrency of the task, and then the actual development is often used.

  

    0), ^{        [self testserialqueue];        NSLog (@ "%@", [Nsthread CurrentThread]);    });

- (void) testserialqueue{dispatch_queue_t serialqueue= Dispatch_queue_create ("Com.gcd.demo", dispatch_queue_concurrent); Dispatch_async (Serialqueue,^{[Nsthread sleepfortimeinterval:3]; NSLog (@"Task 1%@", [Nsthread CurrentThread]);        }); Dispatch_async (Serialqueue,^{[Nsthread sleepfortimeinterval:1]; NSLog (@"Task 2%@", [Nsthread CurrentThread]);        }); Dispatch_async (Serialqueue,^{[Nsthread sleepfortimeinterval:2]; NSLog (@"Task 3%@", [Nsthread CurrentThread]);        }); Dispatch_async (Serialqueue,^{[Nsthread sleepfortimeinterval:3]; NSLog (@"Task 4%@", [Nsthread CurrentThread]); });}

Execution Result:

2016-08-04 18:07:39.746 gcdtestdemo[7069:1619191] <nsthread:0x15e56cf0>{number = 2, name = (NULL)}

2016-08-04 18:07:40.752 gcdtestdemo[7069:1619197] Task 2 <nsthread:0x15d7dd00>{number = 3, name = (null)}

2016-08-04 18:07:41.752 gcdtestdemo[7069:1619196] Task 3 <nsthread:0x15d514f0>{number = 4, name = (null)}

2016-08-04 18:07:42.751 gcdtestdemo[7069:1619198] Task 1 <nsthread:0x15d79b90>{number = 5, name = (null)}

2016-08-04 18:07:42.752 gcdtestdemo[7069:1619199] Task 4 <nsthread:0x15d644e0>{number = 6, name = (null)}

    

Understanding of IOS GCD (i)

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.