OC dispatch_sync和dispatch_async

今天突然发现同步异步和我想的不太一样,特地记录一下。

在主线程执行:

    
    dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"主线程执行同步任务到主队列, 挂起当前任务,主线程死锁");
        NSLog(@"current thread is %@", [NSThread currentThread]);
    });
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSLog(@"主线程执行异步任务到主队列, 相当于delay执行");
        NSLog(@"current thread is %@", [NSThread currentThread]);
    });
    
    dispatch_sync(serialQueue, ^{
        NSLog(@"主线程执行同步任务到串行队列, 挂起当前任务,不开启线程,在主线程执行不同任务");
        NSLog(@"current thread is %@", [NSThread currentThread]);
    });
    
    dispatch_sync(concurrentQueue, ^{
        NSLog(@"主线程执行同步任务到并行队列, 挂起当前任务,不开启线程,在主线程执行同步任务");
        NSLog(@"current thread is %@", [NSThread currentThread]);
    });
    
    dispatch_async(serialQueue, ^{
        NSLog(@"主线程执行异步任务到串行队列,开启新线程,在新线程执行任务");
        NSLog(@"current thread is %@", [NSThread currentThread]);
    });
    
    dispatch_async(concurrentQueue, ^{
        NSLog(@"主线程执行异步任务到并行队列,开启新线程,在新线程执行任务");
        NSLog(@"current thread is %@", [NSThread currentThread]);
    });

在子线程执行:


        dispatch_async(concurrentQueue, ^{
            NSLog(@"当前是子线程");
            NSLog(@"current thread is %@", [NSThread currentThread]);
            for (int i = 0; i < 10; i++) {
                dispatch_sync(dispatch_get_main_queue(), ^{
                    NSLog(@"在子线程中执行同步任务到组队列, 挂起当前任务,到主线程执行");
                    NSLog(@"在子线程中执行同步任务到主队列, 挂起当前任务,到主线程执行 current thread is %@", [NSThread currentThread]);
                });
            }
            for (int i = 0; i < 10; i++) {
                dispatch_sync(serialQueue, ^{
                    NSLog(@"在子线程中执行同步任务到串行队列,不开启新线程,在当前当先线程执行");
                    NSLog(@"current thread is %@", [NSThread currentThread]);
                });
            }
            for (int i = 0; i < 10; i++) {
                dispatch_sync(concurrentQueue, ^{
                    NSLog(@"在子线程执行同步任务到并行队列,不开启新线程,在当前线程执行");
                    NSLog(@"current thread is %@", [NSThread currentThread]);
                });
            }
            for (int i = 0; i < 10; i ++) {
                dispatch_async(serialQueue, ^{
                    NSLog(@"在子线程异步执行到串行队列,在另外一个子线程10个任务依次执行");
                    NSLog(@"current thread is %@", [NSThread currentThread]);
                });
            }
            for (int i = 0; i < 10; i ++) {
                dispatch_async(concurrentQueue, ^{
                    NSLog(@"在子线程执行异步任务到并行队列,可能会开启子线程,也有可能在原来线程执行");
                    NSLog(@"current thread is %@", [NSThread currentThread]);
                });
            }
        });

总结:

  • 在当前线程中执行同步任务到其他队列,不开启子线程,在当前线程执行;除非队列为组队列,会在主线程执行;
  • 在当前线程中执行异步任务到其他队列,一般会开启子线程,也有可能在当前线程执行
  • 同步和异步的唯一区别是,会不会暂停当前任务

你可能感兴趣的:(OC dispatch_sync和dispatch_async)