GCD练习
ios 多线程 GCD : ios 多线程
- 全剧队列,异步执行
- 线程间通信
- 信号量
- 文件锁
- 单利模式
- 延时执行
- 取消任务
- 多核心遍历数组
- 队列组
- 消息传递机制
0.关于队列
1).创建或获取队列
// 获取全剧队列,并发队列
//第一个参数为:优先级,第二个参数为:苹果保留参数,写0即可。
#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
// 获取主队列,串行队列
dispatch_queue_t queue = dispatch_get_main_queue();
// 创建队列 串行队列(DISPATCH_QUEUE_SERIAL 或者 NULL),并发队列(DISPATCH_QUEUE_CONCURRENT)
// 第一个参数为:队列名字,第二个参数为:队列类型,如上
dispatch_queue_t queue = dispatch_queue_create("name", DISPATCH_QUEUE_CONCURRENT)
1.全剧队列,异步执行
- (void)test7{
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
for (int i = 0 ; i < 20; i++) {
dispatch_async(queue, ^{
NSLog(@"%@ - %d",[NSThread currentThread],i);
});
}
dispatch_suspend(queue);
}
2. 线程间通信
- (void)gcd{
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
for (int i =0 ; i
3. 信号量
1).信号量控制线程最大并发数量
- (void)test4{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(10);//为了让一次输出10个,初始信号量为10
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
for (int i = 0; i <100; i++)
{
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);//每进来1次,信号量-1;进来10次后就一直hold住,直到信号量大于0;
dispatch_async(queue, ^{
NSLog(@"%i - %@",i,[NSThread currentThread]);
sleep(2);
dispatch_semaphore_signal(semaphore);//由于这里只是log,所以处理速度非常快,我就模拟2秒后信号量+1;
});
}
}
4. 文件锁
- (void)test7{
// 必须传DISPATCH_QUEUE_CONCURRENT,不然和dispatch_async一样
dispatch_queue_t queue = dispatch_queue_create("kk", DISPATCH_QUEUE_CONCURRENT);
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
// 这里可以是异步读文件
for (int i = 0; i<10; i++) {
dispatch_async(queue, ^{
NSLog(@"111 - %@ - %d",[NSThread currentThread],i);
});
}
// 只允许一个线程在,一个时间,修改数据
dispatch_barrier_async(queue, ^{
NSLog(@"%@ - 2222",[NSThread currentThread]);
});
// 其他线程异步读取,修改后的数据
for (int i = 0; i<10; i++) {
dispatch_async(queue, ^{
NSLog(@"333 - %@ - %d",[NSThread currentThread],i);
});
}
}
5. 单利模式
static SingletonTimer * instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[SingletonTimer alloc] init];
});
return instance;
6. 延时执行
- (void)test7{
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC));
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_after(time, queue, ^{
NSLog(@"%@",[NSThread currentThread]);
});
}
7. 取消任务
- (void)test8{
dispatch_queue_t concurrentDispatchQueue = dispatch_queue_create("com.test.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(concurrentDispatchQueue, ^{
for (int i=0; i<100; i++)
{
NSLog(@"%i",i);
if (i==50)
{
NSLog(@"-----------------------------------");
dispatch_suspend(concurrentDispatchQueue);
sleep(3);
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_resume(concurrentDispatchQueue);
});
}
}
});
}
// 我们甚至可以在不同的线程对这个队列进行挂起和恢复,因为GCD是对队列的管理。
8. 多核心遍历数组
- (void)test11{
NSArray *arr = [[NSArray alloc] initWithObjects:@"1",@"2",@"3", nil];
dispatch_apply(arr.count, dispatch_get_global_queue(0, 0), ^(size_t i) {
NSLog(@"%@ - %zu",[NSThread currentThread],i);
});
NSLog(@"over %@",[NSThread currentThread]);
}
9. 队列组
- (void)test9{
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queueAll1 = dispatch_queue_create("hahaha1", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queueAll2 = dispatch_queue_create("hahaha2", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queueAll1, ^{
for (int i =0 ; i<10; i++) {
NSLog(@"%s - %d - %@",dispatch_queue_get_label(queueAll1),i,[NSThread currentThread]);
}
});
dispatch_group_async(group, queueAll2, ^{
for (int i =0 ; i<10; i++) {
[NSThread sleepForTimeInterval:1.0];
NSLog(@"%s - %d - %@",dispatch_queue_get_label(queueAll2),i,[NSThread currentThread]);
}
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
for (int i =0 ; i<10; i++) {
NSLog(@"%s - %d - %@",dispatch_queue_get_label(dispatch_get_main_queue()),i,[NSThread currentThread]);
}
});
}
10. 消息传递机制
- (void)test12{
//创建一个数据对象,DISPATCH_SOURCE_TYPE_DATA_ADD的含义表示数据变化时相加
source = dispatch_source_create(DISPATCH_SOURCE_TYPE_DATA_ADD, 0, 0, dispatch_get_main_queue());
//创建接收数据变化的句柄
dispatch_source_set_event_handler(source, ^{
NSLog(@"%lu",dispatch_source_get_data(source));
});
//启动
dispatch_resume(source);
//设置数据
dispatch_source_merge_data(source, 2);
//这步执行完之后会执行打印方法
}