iOS GCD随记(一)任务/队列组合

    任务的执行方式分为同步执行和异步执行,队列分为串行队列和并行队列,加一个特殊的队列:主队列。根据任务的执行方式和队列的类型,我们可以得到如下的六种搭配方式:

1.同步执行+串行队列
2.同步执行+并行队列
3.异步执行+串行队列
4.异步执行+并行队列
5.同步执行+主队列
6.异步执行+主队列

1.同步执行+串行队列

在当前线程中执行任务,不会开启新的线程,每个任务顺序执行。

- (void)syncSerial {
    
    NSLog(@"currentThread : %@", [NSThread currentThread]);
    NSLog(@"---syncSerial begin---");
    
    dispatch_queue_t queue = dispatch_queue_create("com.yang.dispatch", DISPATCH_QUEUE_SERIAL);
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务1----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务2----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务3----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    NSLog(@"---syncSerial end---");
}

控制台输出:
2019-09-23 16:18:28.433765+0800 test[3877:148401] currentThread : {number = 1, name = main}
2019-09-23 16:18:28.433915+0800 test[3877:148401] ---syncSerial begin---
2019-09-23 16:18:31.434322+0800 test[3877:148401] 任务1----currentThread : {number = 1, name = main}
2019-09-23 16:18:34.435545+0800 test[3877:148401] 任务2----currentThread : {number = 1, name = main}
2019-09-23 16:18:37.435939+0800 test[3877:148401] 任务3----currentThread : {number = 1, name = main}
2019-09-23 16:18:37.436181+0800 test[3877:148401] ---syncSerial end---

从同步执行 + 串行队列中看出:

1.所有任务都是在当前线程(主线程)中执行的,没有开启新的线程(同步执行不具备开启新线程的能力)。
2.所有任务都在打印的---syncSerial begin---和---syncSerial end---之间执行的(同步任务需要等待队列的任务执行结束)。
3.任务按顺序执行的。串行队列中,后一个任务等待前一个任务执行完成后才继续执行,同一时间只有一个任务被执行。

2.同步执行+并行队列

和串行队列相同,在当前线程中执行任务,不会开启新的线程,每个任务顺序执行。

- (void)syncConcurrent {
    
    NSLog(@"currentThread : %@", [NSThread currentThread]);
    NSLog(@"---syncConcurrent begin---");
    
    dispatch_queue_t queue = dispatch_queue_create("com.yang.dispatch", DISPATCH_QUEUE_CONCURRENT);
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务1----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务2----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务3----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    NSLog(@"---syncConcurrent end---");
}

控制台输出:
2019-09-23 16:40:36.823352+0800 test[3940:161433] ---syncConcurrent begin---
2019-09-23 16:40:39.823935+0800 test[3940:161433] 任务1----currentThread : {number = 1, name = main}
2019-09-23 16:40:42.825411+0800 test[3940:161433] 任务2----currentThread : {number = 1, name = main}
2019-09-23 16:40:45.826935+0800 test[3940:161433] 任务3----currentThread : {number = 1, name = main}
2019-09-23 16:40:45.827237+0800 test[3940:161433] ---syncConcurrent end---

从同步执行+并行队列中看出:

1.所有任务都是在当前线程(主线程)中执行的,没有开启新的线程(同步执行不具备开启新线程的能力)。
2.所有任务都在打印的---syncConcurrent begin---和---syncConcurrent end---之间执行的(同步任务需要等待队列的任务执行结束)。
3.任务按顺序执行的。并行队列中, 虽然可以在多个线程中同时执行多个任务,但是同步执行并不具备开启新线程的能力,所以依旧是在当前线程中顺序执行任务。

3.异步执行+串行队列

会开启新线程,在新线程中(一个),每个任务会顺序执行。不会阻塞主线程的任务。

- (void)asyncSerial {
    
    NSLog(@"currentThread : %@", [NSThread currentThread]);
    NSLog(@"---asyncSerial begin---");
    
    dispatch_queue_t queue = dispatch_queue_create("com.yang.dispatch", DISPATCH_QUEUE_SERIAL);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务1----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务2----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务3----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    NSLog(@"---asyncSerial end---");
}

控制台输出:
2019-09-23 16:49:25.571909+0800 test[3996:167610] currentThread : {number = 1, name = main}
2019-09-23 16:49:25.572061+0800 test[3996:167610] ---asyncSerial begin---
2019-09-23 16:49:25.572216+0800 test[3996:167610] ---asyncSerial end---
2019-09-23 16:49:28.572296+0800 test[3996:167764] 任务1----currentThread : {number = 5, name = (null)}
2019-09-23 16:49:31.576001+0800 test[3996:167764] 任务2----currentThread : {number = 5, name = (null)}
2019-09-23 16:49:34.577384+0800 test[3996:167764] 任务3----currentThread : {number = 5, name = (null)}

从异步执行+串行队列中看出:

1.开启了新线程(异步执行具备开启新线程的能力)。只开启了一条新线程(串行队列里的任务顺序执行,所以只会开启一条新的线程)。
2.所有任务是在打印的---asyncSerial begin---和---asyncSerial end---之后才开始执行的(异步执行会开启新的线程执行任务,并不会阻塞主线程中的任务执行)。
3.任务是按顺序执行的。串行队列中,后一个任务等待前一个任务执行完成后才继续执行,同一时间只有一个任务被执行。

4.异步执行+并行队列

会开启新线程,在新线程中(多个),每个任务会同时执行。不会阻塞主线程的任务。

- (void)asyncConcurrent {
    
    NSLog(@"currentThread : %@", [NSThread currentThread]);
    NSLog(@"---asyncConcurrent begin---");
    
    dispatch_queue_t queue = dispatch_queue_create("com.yang.dispatch", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务1----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务2----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务3----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    NSLog(@"---asyncConcurrent end---");
}

控制台输出:
2019-09-23 17:01:33.505478+0800 test[4029:174530] currentThread : {number = 1, name = main}
2019-09-23 17:01:33.505642+0800 test[4029:174530] ---asyncConcurrent begin---
2019-09-23 17:01:33.505792+0800 test[4029:174530] ---asyncConcurrent end---
2019-09-23 17:01:36.509514+0800 test[4029:174689] 任务1----currentThread : {number = 3, name = (null)}
2019-09-23 17:01:36.509513+0800 test[4029:174685] 任务2----currentThread : {number = 5, name = (null)}
2019-09-23 17:01:36.509513+0800 test[4029:174686] 任务3----currentThread : {number = 4, name = (null)}

从异步执行+并行队列中看出:

1.开启了新线程(异步执行具备开启新线程的能力)。开启了多条新线程(并行队列任务同时执行,所以开启了多条新线程)。
2.所有任务是在打印的---asyncConcurrent begin---和---asyncConcurrent end---之后才开始执行的(异步执行会开启新的线程执行任务,并不会阻塞主线程中的任务执行)。
3.任务是同时执行的。并行队列中, 任务同时执行。

5.同步执行+主队列

程序崩溃

- (void)syncMainQueue {
    
    NSLog(@"currentThread : %@", [NSThread currentThread]);
    NSLog(@"---syncMainQueue begin---");
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务1----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务2----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_sync(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务3----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    NSLog(@"---syncMainQueue end---");
}

控制台输出:
2019-09-23 17:23:47.300561+0800 test[4190:187787] currentThread : {number = 1, name = main}
2019-09-23 17:23:47.300726+0800 test[4190:187787] ---syncMainQueue begin---
(lldb) Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)

原因:任务1、任务2、任务3都被添加到主队列中,在执行时会在主线程中执行,同步自行会让这三个任务等待syncMainQueue执行完毕后才执行。syncMainQueue任务中包含任务1,导致两个任务互相等待导致线程卡死程序崩溃。

解决方法:在新的线程中执行该操作。

控制台输出:
2019-09-23 17:35:57.442233+0800 test[4235:195266] currentThread : {number = 3, name = (null)}
2019-09-23 17:35:57.442383+0800 test[4235:195266] ---syncMainQueue begin---
2019-09-23 17:36:00.453910+0800 test[4235:195106] 任务1----currentThread : {number = 1, name = main}
2019-09-23 17:36:03.456229+0800 test[4235:195106] 任务2----currentThread : {number = 1, name = main}
2019-09-23 17:36:06.466841+0800 test[4235:195106] 任务3----currentThread : {number = 1, name = main}
2019-09-23 17:36:06.467133+0800 test[4235:195266] ---syncMainQueue end---

从异步执行+主队列中看出:

1.未开启新线程。放在主队列中的任务都在主线程中被执行。
2.所有任务是在打印的---syncMainQueue begin---和---syncMainQueue end---之间才开始执行的(同步执行不具备开启新线程的能力)。
3.任务按顺序执行的。因为主队列是串行队列,在串行队列中,后一个任务等待前一个任务执行完成后才继续执行,同一时间只有一个任务被执行。

6.异步执行+主队列

在当前线程中执行任务,不会开启新的线程,每个任务顺序执行。

- (void)asyncMainQueue {
    
    NSLog(@"currentThread : %@", [NSThread currentThread]);
    NSLog(@"---asyncMainQueue begin---");
    
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务1----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务2----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    dispatch_async(queue, ^{
        [NSThread sleepForTimeInterval:3];                              // 耗时操作
        NSLog(@"任务3----currentThread : %@", [NSThread currentThread]); // 线程打印
    });
    
    NSLog(@"---asyncMainQueue end---");
}

控制台输出:
2019-09-23 17:12:39.329922+0800 test[4154:181624] currentThread : {number = 1, name = main}
2019-09-23 17:12:39.330079+0800 test[4154:181624] ---asyncMainQueue begin---
2019-09-23 17:12:39.330242+0800 test[4154:181624] ---asyncMainQueue end---
2019-09-23 17:12:42.341870+0800 test[4154:181624] 任务1----currentThread : {number = 1, name = main}
2019-09-23 17:12:45.343171+0800 test[4154:181624] 任务2----currentThread : {number = 1, name = main}
2019-09-23 17:12:48.343738+0800 test[4154:181624] 任务3----currentThread : {number = 1, name = main}

从异步执行+主队列中看出:

1.未开启新线程。放在主队列中的任务都在主线程中被执行。
2.所有任务是在打印的---asyncMainQueue begin---和---asyncMainQueue end---之后才开始执行的(异步执行会开启新的线程执行任务,并不会阻塞主线程中的任务执行)。
3.任务按顺序执行的。因为主队列是串行队列,在串行队列中,后一个任务等待前一个任务执行完成后才继续执行,同一时间只有一个任务被执行。

总结:

总结 串行队列 并行队列 主队列
同步执行 未开启新线程,顺序执行。 未开启新线程,顺序执行。 主线程中执行,造成死锁。子线程中执行,在主线程中顺序执行。
异步执行 开启新线程,顺序执行。 开启新线程,同时执行。 未开启新线程,顺序执行

你可能感兴趣的:(iOS GCD随记(一)任务/队列组合)