GCD全解-05-dispatch_apply-重复提交操作

GCD系列是阅读官方文件和在实践中总结的一些常见的GCD用法,基本涉及全部的GCD内容形成的集合文章,文章重点在与精简和全面覆盖。

学习本集合你可以了解:
1. GCD是如何做到多线程调度的
2. 对比其他iOS的多线程方案,GCD的优势和劣势
3. 如何使用GCD相关的API

我将在后续不断补充详细内容和实际案例, 欢迎关注,提问和讨论

01-dispatch-iOS系统调度

02-dispatch_queue-调度队列

03-dispatch_after/dispatch_time-延迟调度和操作

04-dispatch_barrier_sync/async-线程阻塞

05-dispatch_apply-重复提交操作

06-dispatch_once-单次执行

07-dispatch_semaphore-信号量/数据同步

08-dispatch_group-调度组/多异步操作控制

09-dispatch_block-GCD取消操作

10-dispatch_source-调度资源

11-GCD死锁

重复执行某个任务,但是注意这个方法没有办法异步执行
(为了不阻塞线程可以使用dispatch_async()包装一下再执行

* @abstract
 * Submits a block to a dispatch queue for parallel invocation.
   #  提交一个Block到队列并发调用

 * @discussion
 * Submits a block to a dispatch queue for parallel invocation. This function
 * waits for the task block to complete before returning. If the specified queue
 * is concurrent, the block may be invoked concurrently, and it must therefore
 * be reentrant safe.
 *
 * Each invocation of the block will be passed the current index of iteration.
 *
 * @param iterations The number of iterations(反复) to perform.
 *
 * @param queue
 * The dispatch queue to which the block is submitted.
 * The preferred value to pass is DISPATCH_APPLY_AUTO to automatically use
 * a queue appropriate for the calling thread.

void dispatch_apply(size_t iterations, dispatch_queue_t queue, DISPATCH_NOESCAPE void (^block)(size_t));

demo

dispatch_apply(3, dispatch_queue_create("d", NULL), ^(size_t index) {
        NSLog(@"copy-%ld", index);
});
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-0
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-1
2016-11-05 17:01:38.138 Multithreading[16124:481707] copy-2


dispatch_apply(3, dispatch_queue_create("c", DISPATCH_QUEUE_CONCURRENT), ^(size_t index) {
        NSLog(@"copy-%ld", index);
});
2016-11-05 17:04:10.798 Multithreading[16153:483197] copy-2
2016-11-05 17:04:10.798 Multithreading[16153:483181] copy-1
2016-11-05 17:04:10.798 Multithreading[16153:483141] copy-0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值