GCD face questions

Today we explain a few interview questions --GCD programming these days encounter. The questions were very good, very elegant about the basic concepts and the use of the GCD.

For basic concepts, my blog has been said earlier, this chapter show to interview questions to explain. We can look at my basic explanation regarding the GCD  https://www.cnblogs.com/guohai-stronger/p/9038567. html

The core program is the GCD queue dispatch, dispatch block the execution will eventually placed in a queue to execute our direct title to explain:

Example 1 [1] GCD face questions

- (void)viewDidLoad {
    [super viewDidLoad];
    [self testGCD];
}

- (void)testGCD {
    //并行队列
    dispatch_queue_t queue = dispatch_queue_create("zxy", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");
}

Explain :

  1. First look at the queue is created parallel queues, no waiting;
  2. First print , and then look dispatch_async, seen as a whole code segment dispatch_async things, the parallel queue, and does not wait, then around dispatch_async things, the following contents, performing the following things again after executing content;
  3. Printed out followed by , 5 executing the print again executes dispatch_async entire code segment, see inside the contents of the print , there is a dispatch_async, and print out 4
       NSLog(@"2");
        dispatch_async(queue, ^{
                NSLog(@"3");
            });
        NSLog(@"4");
  4. Also, because dispatch_async, and print out the back 4, and a parallel queue, asynchronously executed, it will first execute printing 4
  5. After performing print out 4, will be executed 3

Screenshot validation results :

 

[Example] 2 GCD face questions 2

- (void)viewDidLoad {
    [super viewDidLoad];
    [self testGCD];
}

- ( void ) {testGCD
     // serial line queue 
    dispatch_queue_t = dispatch_queue_create Queue ( " ZXY " , DISPATCH_QUEUE_SERIAL );
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_sync(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");
}

explain:

  1. First, the serial queue is only executed in the order
  2. First print out 1  , then dispatch_async will open thread, which content will be slower execution, execute the following print 5 
  3. Print out the 5 , then return to dispatch_async inside, asynchronous execution threads will be open, and found NSLog (@ "2"), dispatch_syncy is a synchronous execution, NSLog (@ "4") , and then print out 2 
  4. Because the outside dispatch_async, print out after 2 and began walking NSLog (@ "4"), and then back in sync inside NSLog (@ "3"), and then we found dispatch_syncy is a synchronous execution, which is synchronized inside NSLog does not perform complete (@ "3"), it is impossible to go NSLog (@ "4"), so that both sides are waiting for each other, resulting in a deadlock .
  5. In the program, crash

Screenshot validation results :

 [Example] 3 GCD face questions 3

dispatch_queue_t queue = dispatch_queue_create("zxy", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        NSLog ( @ " Ha ha ha 1 " );
    });
    dispatch_async(queue, ^{
        NSLog ( @ " Ha ha ha 2 " );
    });
    dispatch_sync(queue, ^{
        NSLog ( @ " Ha ha ha 3 " );
    });
//////////////////// NSLog (
@ " Ha ha ha 0 " ); dispatch_async(queue, ^{ NSLog ( @ " Ha ha ha 7 " ); }); dispatch_async(queue, ^{ NSLog ( @ " Ha ha ha 8 " ); }); dispatch_async(queue, ^{ NSLog ( @ " Ha ha ha 9 " ); });

A 1230789   B 1237890  C 3120798  D 2137890  C 3012789

The above five kinds of answers, which one is right?

explain:

  1. The first is the parallel queue DISPATCH_QUEUE_CONCURRENT, parallel execution of tasks
  2. Synchronization is performed in 3 ha, so this code before the latter 3 is not implemented completely impossible to perform, while the foregoing ha 1 and 2 are asynchronous ha, is finished executing, but when ha 3 executing the will be executed immediately 0 ha ha ha, it is unlikely to occur before executing the 7,8,9 0
  3. Can be judged from the A C E is possible

The above code to run xcode obtained as follows: E answer

[Example] 4 4 GCD face questions

We will DISPATCH_QUEUE_CONCURRENT changed DISPATCH_QUEUE_SERIAL

Because of the parallel to serial queue queues, all tasks performed step by step, so the results are as follows (not to be explained)

 

[Example 5] 5 GCD face questions

- (void) testGCD{
    __block NSInteger a = 0;
    while (a < 100) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            a++;
            NSLog(@"%ld======%@", a, [NSThread currentThread]);
        });
    }
    NSLog(@"卧槽无情%ld", a);
}

讲解:

  1. 首先不可能打印出0,因为有while循环,while不走完,是不可能走后面的.a = 0, 然后进入while循环, 因为是异步,所以就会开启线程执行a ++ , 可能开出的线程状态不好,所以a可能继续为0, ……
  2. 但是在某个时刻,可能线程64状态返回,执行a++, 所以a = 1 ,……
  3. 所以a的最后值是大于等于100(>=100)

可能状态还不错,从上面看出结果为100

 

拓展 下面有几道同样的类似GCD面试题目,结果给大家,大家自己体会

【例6】GCD面试题6

- (void) testGCD2 {
    dispatch_queue_t queue = dispatch_queue_create("zxy", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_sync(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");
}

结果如下:

【例7】GCD面试题7

- (void) testGCD2 {
    dispatch_queue_t queue = dispatch_queue_create("zxy", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");
}

结果如下:

【例8】GCD面试题8

- (void) testGCD2 {
    dispatch_queue_t queue = dispatch_queue_create("zxy", DISPATCH_QUEUE_SERIAL);
    NSLog(@"1");
    dispatch_async(queue, ^{
        NSLog(@"2");
        dispatch_async(queue, ^{
            NSLog(@"3");
        });
        NSLog(@"4");
    });
    NSLog(@"5");
}

结果如下:

 

以上就是GCD的经典面试题,其实看了这几道面试题,发现还都是GCD的基本内容,串行队列,并行队列,以及同步异步是否开启线程的基本概念,希望通过本次讲解,大家对GCD的认识会更上一层,谢谢!!!

Guess you like

Origin www.cnblogs.com/guohai-stronger/p/11900622.html