• Case 1
- (void)interview01 {// question: If the following code is executed on the main thread, will it cause deadlock? Will! NSLog(@"viewDid1"); dispatch_queue_t queue = dispatch_get_main_queue(); Dispatch_sync (queue, ^{NSLog(@" perform task 2"); }); NSLog(@"ViewDid3"); // dispatch_sync executes tasks in the current thread immediately}Copy the code
  • The results of

  • Analysis of the

    • The “Execute Task 2” task is added to the main queue (special serial queue), the current thread is the main thread, and the task is added to the main thread for execution
    • Sync does not start new threads. Synchronous serial
    • So it blocks and waits for the previous task in the main thread to complete before it can execute the next task.
    • Dispatch_sync immediately synchronizes tasks on the current thread and executes tasks only after they are executed.
    • During the execution of task 1, task 2 should be executed, and task 1 should be followed by task 1 after task 2 is executed. However, the logic of the machine is that task 2 can only be executed after task 1 is executed, and task 1 can only be continued after task 2 is executed. As a result, task 1 and task 2 are waiting for each other to complete execution. A deadlock.
  • Case 2

- (void)interview02 {// question: The following code is executed on the main thread, does it cause deadlock? Don't! NSLog(@" Perform task 1"); dispatch_queue_t queue = dispatch_get_main_queue(); Dispatch_async (queue, ^{NSLog(@" perform task 2"); }); NSLog(@" Perform task 3"); // dispatch_async does not require immediate synchronous execution of tasks in the current thread}Copy the code
  • Asynchronous serial, no new thread opened
  • The result is no deadlock.

  • Analysis of the
    • ViewdidLoad is task 1 executing
    • Task 2 is added, but this task is executed asynchronously and can wait first
    • Wait until task 1 is complete before doing task 2
    • I’ve added a viewwillAppear here to show that this print 2 is waiting for viewDidLoad to finish executing.

  • Case 3
- (void)interview03 {// question: The following code is executed on the main thread, does it cause deadlock? Will! NSLog(@" Perform task 1"); dispatch_queue_t queue = dispatch_queue_create("myqueu", DISPATCH_QUEUE_SERIAL); Dispatch_async (queue, ^{// 0 NSLog(@" task 2")); Dispatch_sync (queue, ^{// 1 NSLog(@" perform task 3"); }); NSLog(@" Perform task 4"); }); NSLog(@" Perform task 5"); }Copy the code
  • The results of

  • Analysis of the
    • This analysis is similar to the first one

  • Case 4
{// question: The following code is executed on the main thread, does it cause a deadlock? Don't! NSLog(@" Perform task 1"); Dispatch_queue_t queue2 = dispatch_queue_create("myqueu2", DISPATCH_QUEUE_CONCURRENT); dispatch_queue_create("myqueu2", DISPATCH_QUEUE_CONCURRENT); Dispatch_async (queue2, ^{// 0 NSLog(@" task 2"); Dispatch_sync (queue2, ^{// 1 NSLog(@" perform task 3"); }); NSLog(@" Perform task 4"); }); NSLog(@" Perform task 5"); }Copy the code

conclusion

When executing tasks synchronously in a serial queue, do not add new tasks synchronously to a task, as this will cause a deadlock.

The queue

dispatch_queue_t queue1 = dispatch_get_global_queue(0, 0);
dispatch_queue_t queue2 = dispatch_get_global_queue(0, 0);
dispatch_queue_t queue3 = dispatch_queue_create("queu3", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue4 = dispatch_queue_create("queu4", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue5 = dispatch_queue_create("queu5", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue6 = dispatch_queue_create("queu6", DISPATCH_QUEUE_SERIAL);
Copy the code

  • Global concurrent queue
  • Concurrent queue
  • Serial queues