Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ iOS GCD multithreading simple to use     - OpenWrt modify flash size (Linux)

- Questions about Linux compiler u-boot (Programming)

- CKEditor + SWFUpload achieve a more powerful editor (Linux)

- Linux find command detailing (Linux)

- Add local search to your Android app (Programming)

- iOS used in the development --UITabBarController tag controller (Programming)

- Development environment to build MEAN In Ubuntu 15.10 (Server)

- Linux directory configuration (Linux)

- a virtual machine created migrated to host RHEL6.4 on Ubuntu 14.04 (Linux)

- Oracle 11g tracking and monitoring system-level triggers to drop misuse (Database)

- Pydev installed and configured on the Eclipse (Linux)

- Linux system installation Gitlab (Server)

- The best known for archiving / compression tool under linux (Linux)

- Linux common commands MEMO (Linux)

- Formatting Java floating-point types (Programming)

- How to install Nginx on FreeBSD 10.2 as an Apache reverse proxy (Server)

- Linux (Ubuntu) How iptables port mapping (Server)

- BackTrack (BT3, BT4) Linux installation tutorial (Linux)

- To configure parameter configuration and software installation and uninstallation under Linux (Linux)

- Java implementation file encryption and decryption (Programming)

 
         
  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:      
 
- Linux screen commonly commands (Linux)
- Linux, Apache Web site security settings (Linux)
- Linux environment SSH login password instead of using the RSA Certificate (Linux)
- Python interview must look at 15 questions (Programming)
- Linux rights management (Linux)
- Installation of network monitoring ntopng under CentOS 6.4 (Linux)
- 6 common PHP security attacks (Linux)
- Binding multiple network cards in Linux using command nmcli (Linux)
- Linux compiler installation Redis (Database)
- OpenVPN offsite interconnecting room availability and load balancing solution (Server)
- Linux environment has been running Tomcat how to deploy the new Tomcat (Server)
- pkg-config to use (Linux)
- Android float ball and boot from the start (Programming)
- PL / SQL -> UTL_FILE use presentation package (Database)
- Install and manage Java under mac (Linux)
- MySQL performance comparison of large amounts of data storage (Database)
- To install PostgreSQL 9.4 (Database)
- Based AutoYaST automated installation of SUSE practice (Linux)
- Revive Adserver ad server installation on Ubuntu 15.04 / CentOS7 (Server)
- I use the desktop environment in GNU / Linux combination tool (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.