Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ iOS GCD multithreading simple to use     - Create a project using Android Studio LinearLayout (Programming)

- Use OpenWrt build WDS wireless network extension on V2 WHR-G300N (Linux)

- Linux system network security tools sudo Introduction (Linux)

- Fedora 23 How to install LAMP server (Server)

- Why use Docker (Programming)

- Build Python3.4 + PyQt5.5.1 + Eric6.1.1 development platform under Mac OS X 10.11.1 (Server)

- How to use static, class, abstract method in Python (Programming)

- ORA-01157 & ORA-01110 Troubleshooting (Database)

- Linux Network Programming - signal blocking and shielding (block, unblock) (Programming)

- Linux operating system boot process analysis (Linux)

- Java garbage collection and heap memory layout (Programming)

- Oracle 11gr2 new APPEND_VALUES tips (Database)

- Using C / C ++ extensions Python (Programming)

- Android webView URL redirects affect goBack () (Programming)

- JavaScript, some conclusions about the implicit conversion (Programming)

- C # / iOS / Android Universal Encryption and decryption (Programming)

- Zabbix system email alert Python script (Server)

- Install JDK 1.7 + Eclipse in CentOS 6.4 in (Linux)

- Go powerful development server simple example (Server)

- iptables using summary (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:      
 
- NAT and firewall under Linux (Linux)
- Ubuntu 12.04 install RTL8723BE wireless network card driver (Programming)
- Source code to compile and install MySQL 5.7.9 (Database)
- Summary of Docker mounted directory (Server)
- JITwatch installation under Linux (Linux)
- RAID disk array Description (Linux)
- Ubuntu Server security risk checks (Linux)
- CentOS 6.0 system security level (Linux)
- How to install Git on CentOS 7 (Linux)
- How Glances monitoring system on Ubuntu (Linux)
- Gentoo: !!! existing preserved libs problem (Linux)
- Compare Swift achieve rapid sorting and sorted Methods (Programming)
- struts2 completely the wrong way to capture 404 (Programming)
- to compile FFmpeg In Ubuntu (Linux)
- Spring Boot + Nginx + Tomcat + SSL configuration notes (Server)
- Intrusion prevention network server security maintenance tips (Linux)
- Iptables application layer plug (Linux)
- Ubuntu 14.04 modify environment variables (Linux)
- Python implementation Bursa transition model (Programming)
- Java deserialization test (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.