ios并发会造成什么问题_iOS并发,串行,异步,同步

如何充分的利用计算机的有效资源,在并发之前使用多线程解决,然而很多东西需要程序员来考虑,如何创建合理数目的线程,如何控制线程之间有效运行互不影响。并发很好的解决了这样的问题。在学习的过程中,我们经常看到并发,串行,异步,同步这样的自研。它们之间有如下4种组合:

1.串行+同步

- (void)serialSync {

dispatch_queue_t serialQueue = dispatch_queue_create("com.tian.lawrence", DISPATCH_QUEUE_SERIAL);

NSLog(@"1 - %@", [NSThread currentThread]);

dispatch_sync(serialQueue, ^{

NSLog(@"2 - %@", [NSThread currentThread]);

});

dispatch_sync(serialQueue, ^{

NSLog(@"3 - %@", [NSThread currentThread]);

});

NSLog(@"4 - %@", [NSThread currentThread]);

}

结果:

2017-08-21 08:37:44.619 Chapter2[1772:68997] 1 - {number = 1, name = main}

2017-08-21 08:37:44.619 Chapter2[1772:68997] 2 - {number = 1, name = main}

2017-08-21 08:37:44.620 Chapter2[1772:68997] 3 - {number = 1, name = main}

2017-08-21 08:37:44.620 Chapter2[1772:68997] 4 - {number = 1, name = main}

可以看到该组合,没有创建新的线程,在当前线程执行任务。首先队列是先进先出的原则,当第一个同步任务插入队列时,阻塞线程,需要执行队列中的任务。因为队列中只有它自己,所以执行。同理,第二个第三个...也是这样的情况。

2.串行+异步

- (void)serialAsync {

dispatch_queue_t serialQueue = dispatch_queue_create("com.tian.lawrence", DISPATCH_QUEUE_SERIAL);

NSLog(@"1 - %@", [NSThread currentThread]);

dispatch_async(serialQueue, ^{

NSLog(@"2 - %@", [NSThread currentThread]);

});

dispatch_async(serialQueue, ^{

NSLog(@"3 - %@", [NSThread currentThread]);

});

NSLog(@"4 - %@", [NSThread currentThread]);

}

结果:

2017-08-21 08:37:44.620 Chapter2[1772:68997] 1 - {number = 1, name = main}

2017-08-21 08:37:44.620 Chapter2[1772:68997] 4 - {number = 1, name = main}

2017-08-21 08:37:44.621 Chapter2[1772:69315] 2 - {number = 3, name = (null)}

2017-08-21 08:37:44.621 Chapter2[1772:69315] 3 - {number = 3, name = (null)}

当前组合,只创建了一条新的线程。因为是异步任务,所以在插入队列时,不会阻塞,待插入结束后,然后依次执行队列中的任务。

3.并行+同步

- (void)concurrentSync {

dispatch_queue_t serialQueue = dispatch_queue_create("com.tian.lawrence", DISPATCH_QUEUE_CONCURRENT);

NSLog(@"1 - %@", [NSThread currentThread]);

dispatch_sync(serialQueue, ^{

NSLog(@"2 - %@", [NSThread currentThread]);

});

dispatch_sync(serialQueue, ^{

NSLog(@"3 - %@", [NSThread currentThread]);

});

NSLog(@"4 - %@", [NSThread currentThread]);

}

结果

2017-08-21 08:37:44.621 Chapter2[1772:68997] 1 - {number = 1, name = main}

2017-08-21 08:37:44.621 Chapter2[1772:68997] 2 - {number = 1, name = main}

2017-08-21 08:37:44.622 Chapter2[1772:68997] 3 - {number = 1, name = main}

2017-08-21 08:37:44.622 Chapter2[1772:68997] 4 - {number = 1, name = main}

在当前队列执行任务,没有创建新的线程。

4.并行+异步

- (void)concurrentAsync {

dispatch_queue_t serialQueue = dispatch_queue_create("com.tian.lawrence", DISPATCH_QUEUE_CONCURRENT);

NSLog(@"1 - %@", [NSThread currentThread]);

dispatch_async(serialQueue, ^{

NSLog(@"2 - %@", [NSThread currentThread]);

});

dispatch_async(serialQueue, ^{

NSLog(@"3 - %@", [NSThread currentThread]);

});

NSLog(@"4 - %@", [NSThread currentThread]);

}

结果

2017-08-21 08:37:44.624 Chapter2[1772:68997] 1 - {number = 1, name = main}

2017-08-21 08:37:44.624 Chapter2[1772:68997] 4 - {number = 1, name = main}

2017-08-21 08:37:44.624 Chapter2[1772:69315] 2 - {number = 3, name = (null)}

2017-08-21 08:37:44.624 Chapter2[1772:69312] 3 - {number = 4, name = (null)}

当前组合,按照子任务的数目创建相应数目的线程,达到了并发的目的。因为异步,在插入到队列的时候,不会阻塞。弹出队列的执行时,会创建不同的线程去执行子任务,子任务执行的结果的输出是无序的,子任务之间相互不影响。

附图一张,便于理解:

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值