iOS:多线程

2018-06-05  本文已影响19人  春暖花已开

多线程,任务分为“同步”和“异步”;队列分为“串行”和“并发”。各个组合的结果如下:

区别 并发队列 串行队列 主队列
同步(sync) 没有开启新线程,串行执行任务 没有开启新线程,串行执行任务 主线程调用:死锁卡住不执行;其他线程调用:没有开启新线程,串行执行任务
异步(async) 有开启新线程,并发执行任务 有开启新线程(1条),串行执行任务 没有开启新线程,串行执行任务

一、同步串行

- (IBAction)SyncSerial:(id)sender {
    
    NSLog(@"同步串行 开始执行");
    NSLog(@"%@", [NSThread currentThread]);
    
    dispatch_queue_t serialQueue = dispatch_queue_create("com.LynnZhang.serial", DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(serialQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(serialQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(serialQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"同步串行 执行完毕\n\n");
    
}
执行结果:
2018-06-05 09:48:00.635288+0800 GCDDemo[1058:33785] 同步串行 开始执行
2018-06-05 09:48:00.635871+0800 GCDDemo[1058:33785] <NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:02.636766+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:04.638179+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:06.638876+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:08.640702+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:10.641340+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:12.643052+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:14.643766+0800 GCDDemo[1058:33785] 3---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:16.644485+0800 GCDDemo[1058:33785] 3---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:18.645132+0800 GCDDemo[1058:33785] 3---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 09:48:18.645473+0800 GCDDemo[1058:33785] 同步串行 执行完毕
同步 + 串行 的结论:

二、同步并发

- (IBAction)SyncConcurrent:(id)sender {
    
    NSLog(@"同步并发 开始执行");
    NSLog(@"%@", [NSThread currentThread]);
    
    dispatch_queue_t concQueue = dispatch_queue_create("com.LynnZhang.CONCURRENT", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(concQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(concQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(concQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"同步并发 执行完毕\n\n");
}
执行结果:
2018-06-05 10:13:54.134831+0800 GCDDemo[1058:33785] 同步并发 开始执行
2018-06-05 10:13:54.135298+0800 GCDDemo[1058:33785] <NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:13:56.137100+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:13:58.137728+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:00.139357+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:02.140966+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:04.141455+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:06.142465+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:08.144110+0800 GCDDemo[1058:33785] 3---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:10.144704+0800 GCDDemo[1058:33785] 3---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:12.146347+0800 GCDDemo[1058:33785] 3---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:14:12.146725+0800 GCDDemo[1058:33785] 同步并发 执行完毕
同步 + 并发 的结论:

三、异步串行

NSLog(@"异步串行 开始执行");
    NSLog(@"%@", [NSThread currentThread]);
    
    dispatch_queue_t serialQueue = dispatch_queue_create("com.LynnZhang.serial", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(serialQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(serialQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(serialQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"异步串行 执行完毕\n\n");
执行结果:
2018-06-05 10:21:33.266328+0800 GCDDemo[1058:33785] 异步串行 开始执行
2018-06-05 10:21:33.266845+0800 GCDDemo[1058:33785] <NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:21:33.267315+0800 GCDDemo[1058:33785] 异步串行 执行完毕


2018-06-05 10:21:35.271718+0800 GCDDemo[1058:55130] 1---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:37.277201+0800 GCDDemo[1058:55130] 1---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:39.282826+0800 GCDDemo[1058:55130] 1---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:41.285625+0800 GCDDemo[1058:55130] 2---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:43.291136+0800 GCDDemo[1058:55130] 2---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:45.296771+0800 GCDDemo[1058:55130] 2---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:47.300043+0800 GCDDemo[1058:55130] 3---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:49.301472+0800 GCDDemo[1058:55130] 3---<NSThread: 0x604000276300>{number = 3, name = (null)}
2018-06-05 10:21:51.305281+0800 GCDDemo[1058:55130] 3---<NSThread: 0x604000276300>{number = 3, name = (null)}
异步 + 串行 的结论:

四、异步并发

- (IBAction)AsyncConcurrent:(id)sender {
    
    NSLog(@"异步并发 开始执行");
    NSLog(@"%@", [NSThread currentThread]);
    
    dispatch_queue_t concQueue = dispatch_queue_create("com.LynnZhang.CONCURRENT", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(concQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(concQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(concQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"3---%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"异步并发 执行完毕\n\n");
}
执行结果:
2018-06-05 10:28:33.730984+0800 GCDDemo[1058:33785] 异步并发 开始执行
2018-06-05 10:28:33.731502+0800 GCDDemo[1058:33785] <NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:28:33.732277+0800 GCDDemo[1058:33785] 异步并发 执行完毕


2018-06-05 10:28:35.735018+0800 GCDDemo[1058:59146] 1---<NSThread: 0x604000276c00>{number = 4, name = (null)}
2018-06-05 10:28:35.735018+0800 GCDDemo[1058:35531] 2---<NSThread: 0x604000276b80>{number = 5, name = (null)}
2018-06-05 10:28:35.735088+0800 GCDDemo[1058:59155] 3---<NSThread: 0x600000277e80>{number = 6, name = (null)}
2018-06-05 10:28:37.740332+0800 GCDDemo[1058:35531] 2---<NSThread: 0x604000276b80>{number = 5, name = (null)}
2018-06-05 10:28:37.740332+0800 GCDDemo[1058:59146] 1---<NSThread: 0x604000276c00>{number = 4, name = (null)}
2018-06-05 10:28:37.740332+0800 GCDDemo[1058:59155] 3---<NSThread: 0x600000277e80>{number = 6, name = (null)}
2018-06-05 10:28:39.744781+0800 GCDDemo[1058:35531] 2---<NSThread: 0x604000276b80>{number = 5, name = (null)}
2018-06-05 10:28:39.744783+0800 GCDDemo[1058:59155] 3---<NSThread: 0x600000277e80>{number = 6, name = (null)}
2018-06-05 10:28:39.744783+0800 GCDDemo[1058:59146] 1---<NSThread: 0x604000276c00>{number = 4, name = (null)}
异步 + 并发 的结论:

五、异步主线程

- (IBAction)AsyncMain:(id)sender {
    
    NSLog(@"异步主线程 开始执行");
    NSLog(@"%@", [NSThread currentThread]);
    
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    dispatch_async(mainQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(mainQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"异步主线程 执行完毕");
}
执行结果:
2018-06-05 10:32:49.198181+0800 GCDDemo[1058:33785] 异步主线程 开始执行
2018-06-05 10:32:49.198649+0800 GCDDemo[1058:33785] <NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:32:49.198978+0800 GCDDemo[1058:33785] 异步主线程 执行完毕
2018-06-05 10:32:51.203943+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:32:53.206258+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:32:55.207998+0800 GCDDemo[1058:33785] 1---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:32:57.210170+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:32:59.212391+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
2018-06-05 10:33:01.214465+0800 GCDDemo[1058:33785] 2---<NSThread: 0x6000000661c0>{number = 1, name = main}
异步 + 主线程 的结论:

六、同步主线程(死锁)

- (IBAction)SyncMain:(id)sender {
    
    NSLog(@"同步主线程 开始执行");
    NSLog(@"%@", [NSThread currentThread]);
    
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    dispatch_sync(mainQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"1---%@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(mainQueue, ^{
        for (NSInteger i = 0; i < 3; i++) {
            [NSThread sleepForTimeInterval:2];
            NSLog(@"2---%@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"同步主线程 执行完毕");
}
执行结果:
2018-06-05 10:38:58.565768+0800 GCDDemo[1058:33785] 同步主线程 开始执行
2018-06-05 10:38:58.566234+0800 GCDDemo[1058:33785] <NSThread: 0x6000000661c0>{number = 1, name = main}
(lldb) 
同步 + 主线程 的结论:

在主线程中使用同步执行 + 主队列,追加到主线程的任务1、任务2、任务3都不再执行了,而且同步主线程 执行完毕也没有打印,在XCode 9上还会报崩溃。这是为什么呢?

这是因为我们在主线程中执行syncMain方法,相当于把syncMain任务放到了主线程的队列中。而同步执行会等待当前队列中的任务执行完毕,才会接着执行。那么当我们把任务1追加到主队列中,任务1就在等待主线程处理完syncMain任务。而syncMain任务需要等待任务1执行完毕,才能接着执行。

那么,现在的情况就是syncMain任务和任务1都在等对方执行完毕。这样大家互相等待,所以就卡住了,所以我们的任务执行不了,而且同步主线程 执行完毕也没有打印。

参考:https://www.jianshu.com/p/2d57c72016c6

上一篇 下一篇

猜你喜欢

热点阅读