GCD-3(总结)

GCD总结
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent )event{
[self demo];
}
- (void)demo {
/

1.任务1,2在子线程顺序执行 -->串行 异步 开一个线程 有序
2.完成之后通知主线程执行任务3,4并顺序执行 -->
3.完成之后开启任务5,6,7并发执行
*/
dispatch_queue_t serialQueue = dispatch_queue_create("serial", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t conQ = dispatch_get_global_queue(0, 0);

dispatch_async(serialQueue, ^{
    [self task:1];
});
dispatch_async(serialQueue, ^{
    [self task:2];
});

/*
 2.完成之后通知主线程执行任务3,4并顺序执行 -->主队列 异步
 */
dispatch_sync(serialQueue, ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        [self task:3];
    });
    dispatch_async(dispatch_get_main_queue(), ^{
        [self task:4];
    });
});

/*
 3.完成之后开启任务5,6,7并发执行-->并发 异步
 */
dispatch_async(dispatch_get_main_queue(), ^{
    dispatch_async(conQ, ^{
        [self task:5];
    });
    dispatch_async(conQ, ^{
        [self task:6];
    });
    dispatch_async(conQ, ^{
        [self task:7];
    });
});
}
//任务
- (void)task:(int)number {
NSLog(@"%d %@", number, [NSThread currentThread]);
}

你可能感兴趣的:(GCD-3(总结))