Small valley bottom exploration collection

  • In the studyGCDFirst, here’s oneFunctions and queuesSo that,GCDIt’s easier to understand

Article 1. The concept

1.1. Functions in GCD

    1. Task usingblockencapsulation
    1. The task ofblockThere are no arguments and no return values
    1. A function that performs the task
    • 3.1. The asynchronousdispatch_async

    The next statement can be executed without waiting for the current statement to complete

    Opens the thread to execute the block’s task

    Asynchrony is a byword for multithreading

    • 3.2. The synchronousdispatch_sync

    You must wait for the current statement to complete before executing the next statement

    Thread will not be opened

    The current task of the block is executed

1.2. The queue

    1. Here’s a graphic picture of the queue:

    1. Serial queuesAnalysis: is to queue, just one channel:FIFO (First in, first out)

    1. Parallel linesAnalysis: Suppose you buy food at a window. There are four Windows

The feeling is changed to eat, better understand ~~

1.3. Combination of functions and queues

    1. Synchronous function serial queue
    1. Does not start the thread and executes the task in the current thread
    2. Tasks are executed sequentially, one task after another
    3. You can get clogged up
    1. Synchronous function concurrent queue
    1. Does not start the thread and executes the task in the current thread
    2. One task after another
    1. Asynchronous function serial queue
    1. Start a new thread
    2. One task after another
    1. Asynchronous function concurrent queue
    1. Start the thread and execute the task in the current thread
    2. Tasks are executed asynchronously, in no order, andCPUScheduling the

Application 2.

  • The application of functions and queues is explained by interview questions.

2.1. Interview Question 1

  • Write the following program output
- (void)textDemo1{
    dispatch_queue_t queue = dispatch_queue_create("question1", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@ "1");
    dispatch_async(queue, ^{
        NSLog(@ "2");
        dispatch_sync(queue, ^{
            NSLog(@ "3");
        });
        NSLog(@ "4");
    });
    NSLog(5 "@");

}
Copy the code

Analysis:

  1. This is a parallel queue
  2. Both synchronous and asynchronous are time consuming
  3. So the first one out is the main lineFrom 1 to 5
  4. In parallel queues are synchronous queues, so they are executed sequentially

Answer: the 1-5-2-3-4

2.2. Interview Question 2

  • Write the following program output
- (void)textDemo2{
    // Synchronize the queue
    dispatch_queue_t queue = dispatch_queue_create("question2".NULL);
    NSLog(@ "1");
    // Asynchronous functions
    dispatch_async(queue, ^{
        NSLog(@ "2");
        / / synchronize
        dispatch_sync(queue, ^{
            NSLog(@ "3");
        });
        NSLog(@ "4");
    });
    NSLog(5 "@");
}
Copy the code

Analysis of the

  1. This is a serial queue: #define DISPATCH_QUEUE_SERIAL NULL
  2. The first one out is the main lineFrom 1 to 5
  3. Then the output2.
  4. After that, people got a little confused. I drew a picture:

  1. Piece of taskaddThree tasksAnd thenThree tasksWaiting for theFour tasksPerformed,Four tasksWaiting for thePiece of taskPerformed,Piece of taskWaiting for theThree tasksafter
  2. And that’s what happensA deadlock

Answer: 1-5-2- stuck

2.3. Interview question 3

  • Multiple choice: The following program output might be ()

    A : 1230789

    B : 1237890

    C : 3120798

    D : 2137890

- (void)textDemo3{
    dispatch_queue_t queue = dispatch_queue_create("question3", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{ 
        NSLog(@ "1");
    });
    dispatch_async(queue, ^{
        NSLog(@ "2");
    });
    
    dispatch_sync(queue, ^{
        NSLog(@ "3");
    });
    
    NSLog(@ "0");

    dispatch_async(queue, ^{
        NSLog(@ "7");
    });
    dispatch_async(queue, ^{
        NSLog(8 "@");
    });
    dispatch_async(queue, ^{
        NSLog(@ "9");
    });
    
}
Copy the code

Analysis:

  1. This is aParallel lines.Asynchronous thread 1, 2Synchronous thread 3It is not certain who finishes first
  2. Three tasksisThread synchronization, so,3Must be in0 In front of the
  3. The 7-8-9 missionisAsynchronous threadSo it’s not in order, but it’s definitely there0after

Answer: AC