Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ iOS GCD multithreading simple to use     - Redis data types Introduction (Database)

- Ubuntu 14.04 can be used to create a WIFI hotspot for Android (Linux)

- Binary search is really easy as you think you do (Programming)

- Run two MySQL service on one server (Database)

- You must ask yourself four questions before deploying Docker (Server)

- Digital jQuery scrolling effect (Programming)

- Access clipboard content across multiple vim instances in a terminal (Linux)

- Debian installation (Linux)

- Additional SQL Server 5123 database reported error (Database)

- Basic data types JavaScript type system and the type of packaging (Programming)

- Gitlab installation under CentOS 7 (Linux)

- Linux Network Programming --TCP and UDP datagram type Explanation (Programming)

- Spring AOP (Programming)

- How to adjust the system time CentOS (Linux)

- Linux commands to access the cheat sheet (Linux)

- Linux character device - user mode and kernel mode data transfer data (Linux)

- To convert into a binary search tree sorted doubly linked list (Programming)

- Installation Enpass secure password manager on Ubuntu (Linux)

- A custom implementation of the Android sidebar (Programming)

- Ubuntu 14.04 Nvidia proprietary drivers for install two graphic cards (Linux)

 
         
  iOS GCD multithreading simple to use
     
  Add Date : 2018-11-21      
         
         
         
  In iOS developers, Apple offers three multi-threading technology, namely:

(1) NSThread

(2) NSOperation

(3) GCD

A brief introduction to the use of GCD.

GCD stands for Grand Central Dispatch, can be called Grand Central scheduling. GCD is actually manages a thread pool, how to create a thread, how to recycle threads, as well as the number of threads allocated, these are the GCD controlled. In development, the programmer is not operating threads related matter, the programmer should only need to do an operation to put the appropriate queue inside.


A: Custom queue

There are several GCD queue, where custom queues in two ways: serial and parallel queue queues

 1: Serial queue: queue task will only execute the order, and only once capable of performing a task. That is, after performing a task, will perform the next task.

2: Parallel Queue: You can perform more than one task. For example, there are 10 parallel queue task, perform three tasks at once, which perform these three tasks done, followed by the implementation of the remaining tasks.

Note: either serial or parallel queue queue, they are FIFO (First In First Out) is. That is, either the queue, the queue into the task sooner, the sooner its execution time (in some cases only the end time of task execution is uncertain).

GCD has two modes of operation, namely synchronous and asynchronous operations

1: Synchronous operation: do not open new thread

2: asynchronous operation: will open a new thread

Two operations and two kinds of queues, a combination of four cases, in fact, in the development, some combinations are basically not used. The following applications describe four combinations.

A combination of: a serial queue + sync operation (not the new thread, and the task is executed one by one, so in fact is the order of execution), as follows:

- (Void) gcdDemo1


{

    // Serial synchronous operation queue +

    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_SERIAL);

    for (int i = 0; i <10; ++ i) {

        dispatch_sync (queue, ^ {

            NSLog (@ "% @% d", [NSThread currentThread], i);

        });

    }

}

number = 1, the main thread is described, no new open thread.

A combination of two: + serial asynchronous operations queue (because the task to be executed one by one, but because it is an asynchronous operation, it will open a new thread, all tasks are performed on new thread), as follows:

- (Void) gcdDemo1
{
    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_SERIAL);
    // Serial asynchronous operations queue +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

number = 2, instructions to open a new child thread, but still the order of execution.


A combination of three: + sync queue parallel operation (as the synchronization operation does not open a new thread, so even begin parallel queue multiple tasks at once, but in fact still is that each task is executed on the main thread, and order execution ). Code is as follows:

- (Void) gcdDemo2
{
    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_CONCURRENT);
    // Parallel synchronization tasks queue +
    for (int i = 0; i <10; ++ i) {
        dispatch_sync (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Not open a new thread and executed sequentially.

A combination of four: + parallel queue asynchronous operations (parallel queue will start once more tasks, and asynchronous operation to open a new thread, so the same time may perform multiple tasks simultaneously open multiple threads and ending time for each task is uncertain). Code is as follows:

- (Void) gcdDemo2
{
    dispatch_queue_t queue = dispatch_queue_create ( "gcddemo", DISPATCH_QUEUE_CONCURRENT);
    // Parallel asynchronous task queue +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

We can see, open multiple threads, and the task is not complete execution of the order.

Two: global queue

In order to facilitate the development, Apple has also provided a global queue, the queue is actually a parallel global queue, therefore, the implementation of the results and the results of parallel queues global queue is consistent. Code is as follows:

Global queue + sync tasks:

- (Void) gcdDemo3
{
    dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // Global synchronization task queue +
    for (int i = 0; i <10; ++ i) {
        // Synchronization task
        dispatch_sync (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Global queue + asynchronous tasks:

- (Void) gcdDemo3
{
    dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // Global queue asynchronous tasks +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Three: the main queue

Apple also provides a main queue is a queue, the queue is the main serial queue, but there are differences and serial queue. The main task queue should be executed sequentially on the main thread, there is no concept of asynchronous. In other words, even if the asynchronous task execution queue on the primary, it will not open a new thread.

+ Main queue asynchronous tasks:

- (Void) gcdDemo4
{
    dispatch_queue_t queue = dispatch_get_main_queue ();
    // Main queue asynchronous tasks +
    for (int i = 0; i <10; ++ i) {
        dispatch_async (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

We can see, does not open a new thread and executed sequentially.

The main queue + sync tasks (threads will be blocked), as follows:

- (Void) gcdDemo4
{
    dispatch_queue_t queue = dispatch_get_main_queue ();
    // + Sync main task queue and can block
    for (int i = 0; i <10; ++ i) {
        dispatch_sync (queue, ^ {
            NSLog (@ "% @% d", [NSThread currentThread], i);
        });
    }
}

Blocking reasons:

Main queue itself is a task A (main task), and the task A has not yet finished execution. During the execution of task A, and insert a new synchronization task B. We know that the serial queue, you must first perform a task completed, in order to continue to perform another task. At this time when:

To continue to perform tasks A, we need to first task B executed, if you want to continue with the task B, task A need to first executed, thus creating a blockage.

In development, we should avoid such blocking.
     
         
         
         
  More:      
 
- RHEL6.4 x86_64 build SVN service (Server)
- Spark source code compiler package (Linux)
- Java regular expression syntax (Programming)
- Nginx version information hidden or modified (Server)
- Mhddfs: multiple smaller partitions into one large virtual storage (Linux)
- How to choose the correct HTTP status code (Server)
- Use smartmontools view the health status of hard disk (Linux)
- Android studio multi-channel ultra-compact version of the package (Programming)
- How to manage KVM virtual environments with command-line tools in Linux (Server)
- Python-- for anomalies and reflection of objects articles (Programming)
- bash login and welcome message: / etc / issue, / etc / motd (Linux)
- Differential test piece using MongoDB performance YCSB (Database)
- Red Hat Linux security settings document (Linux)
- Use window.name + iframe cross-domain access to data Detailed (Programming)
- The new task parallel library feature in .NET 4.6 (Programming)
- Experts teach you how to identify the actual functional differences between the firewall (Linux)
- Ubuntu 14.04 Trusty Tahr User How to install Banshee 2.9.1 (Linux)
- Use nice, cpulimit and cgroups limit cpu usage (Linux)
- Close common port to protect server security (Linux)
- DataGuard Standby backup error RMAN-06820 ORA-17629 to solve (Database)
     
           
     
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.