iOS多线程

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

区别并发队列串行队列主队列
同步(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] 同步串行 执行完毕
同步 + 串行 的结论:
  • 1、所有的任务都是在当前线程中,没有开启新线程(同步任务不具有开启新线程的能力);
  • 2、所有任务都打印在同步串行 开始执行同步串行 执行完毕(同步任务需要等待队列的任务执行结束);
  • 3、任务是按顺序执行的(串行队列每次只有一个任务被执行,任务一个接一个按顺序执行)。
二、同步并发
- (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] 同步并发 执行完毕
同步 + 并发 的结论:
  • 1、所有任务都是在当前线程(主线程)中执行的,没有开启新的线程(同步执行不具备开启新线程的能力);
  • 2、所有任务都在打印的同步并发 开始执行同步并发 执行完毕之间执行的(同步任务需要等待队列的任务执行结束);
    -3、任务按顺序执行的。按顺序执行的原因:虽然并发队列可以开启多个线程,并且同时执行多个任务。但是因为本身不能创建新线程,只有当前线程这一个线程(同步任务不具备开启新线程的能力),所以也就不存在并发。而且当前线程只有等待当前队列中正在执行的任务执行完毕之后,才能继续接着执行下面的操作(同步任务需要等待队列的任务执行结束)。所以任务只能一个接一个按顺序执行,不能同时被执行。
三、异步串行
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)}
异步 + 串行 的结论:
  • 1、开启了一条新线程(异步执行具备开启新线程的能力,串行队列只开启一个线程);
  • 2、打印在了异步串行 开始执行异步串行 执行完毕 之后(异步线程不会做任何等待,可以继续执行任务);
  • 3、任务是按顺序执行的(串行队列每次只有一个任务被执行,任务一个接一个按顺序执行)。
四、异步并发
- (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)}
异步 + 并发 的结论:
  • 1、除了当前线程(主线程),系统又开启了3个线程,并且任务是交替/同时执行的。(异步执行具备开启新线程的能力,且并发队列可开启多个线程,同时执行多个任务)。
  • 2、所有任务是在打印的异步并发 开始执行异步并发 执行完毕之后才执行的。说明当前线程没有等待,而是直接开启了新线程,在新线程中执行任务(异步执行不做等待,可以继续执行任务)。
五、异步主线程
- (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}
异步 + 主线程 的结论:
  • 1、所有任务都是在当前线程(主线程)中执行的,并没有开启新的线程(虽然异步执行具备开启线程的能力,但因为是主队列,所以所有任务都在主线程中)。
  • 2、所有任务是在打印的异步主线程 开始执行异步主线程 执行完毕之后才开始执行的(异步执行不会做任何等待,可以继续执行任务)。
    任务是按顺序执行的(因为主队列是串行队列,每次只有一个任务被执行,任务一个接一个按顺序执行)。
六、同步主线程(死锁)
- (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都在等对方执行完毕。这样大家互相等待,所以就卡住了,所以我们的任务执行不了,而且同步主线程 执行完毕也没有打印。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值