GCD
Reference:《Objective-C高级编程》
Dispatch Queue
一、特点:Dispatch Queue按照追加的顺序处理任务。
二、种类:Serial Dispatch Queue 和 Concurrent Dispatch Queue
Serial Dispatch Queue | Concurrent Dispatch Queue | 对比项 |
---|---|---|
等待 | 不等待 | 是否等待执行中任务完成 |
1 | >=1 | 队列执行需要线程数 |
多个Serial Dispatch Queue将并行执行 | 1个Concurrent Dispatch Queue可并行执行多任务 | 并行执行多任务 |
为每个Serial Dispatch Queue分配一个线程 | 取决于当前系统状态,XNU内核决定所需线程数,应当执行的处理数减少时,XNU内核会结束不再需要的线程 | 分配线程数 |
多线程开发注意:
1、数据竞争:多线程更新同一数据资源导致数据不一致。
2、死锁:停止等待的线程会导致多线程相互持续等待。
3、使用太多线程会消耗大量内存。
举例:Serial Dispatch Queue
typedef void(^blk)(void);
-(blk)getTask:(int)x
{
return ^{
NSLog(@"非ui线程%d -->%@",x,[NSThread currentThread]);
[NSThread sleepForTimeInterval:1];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"ui线程%d",x);
});
};
}
//test
dispatch_queue_t mySerialQueue=dispatch_queue_create("SerialQueue",DISPATCH_QUEUE_SERIAL);
blk blk1=[self getTask:1];
blk blk2=[self getTask:2];
blk blk3=[self getTask:3];
blk blk4=[self getTask:4];
dispatch_async(mySerialQueue, blk1);
dispatch_async(mySerialQueue, blk2);
dispatch_async(mySerialQueue, blk3);
dispatch_async(mySerialQueue, blk4);
/*输出:等待、按顺序执行、同一线程
2019-03-10 10:19:38.655232+0800 Block[97332:7774955] 非ui线程1 --><NSThread: 0x600002e94bc0>{number = 3, name = (null)}
2019-03-10 10:19:39.659269+0800 Block[97332:7774918] ui线程1
2019-03-10 10:19:39.659381+0800 Block[97332:7774955] 非ui线程2 --><NSThread: 0x600002e94bc0>{number = 3, name = (null)}
2019-03-10 10:19:40.664956+0800 Block[97332:7774918] ui线程2
2019-03-10 10:19:40.664963+0800 Block[97332:7774955] 非ui线程3 --><NSThread: 0x600002e94bc0>{number = 3, name = (null)}
2019-03-10 10:19:41.670519+0800 Block[97332:7774918] ui线程3
2019-03-10 10:19:41.670520+0800 Block[97332:7774955] 非ui线程4 --><NSThread: 0x600002e94bc0>{number = 3, name = (null)}
2019-03-10 10:19:42.676126+0800 Block[97332:7774918] ui线程4
*/
举例:Concurrent Dispatch Queue
dispatch_queue_t myConcurrentqueue=dispatch_queue_create("ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
blk blk1=[self getTask:1];
blk blk2=[self getTask:2];
blk blk3=[self getTask:3];
blk blk4=[self getTask:4];
dispatch_async(myConcurrentqueue, blk1);
dispatch_async(myConcurrentqueue, blk2);
dispatch_async(myConcurrentqueue, blk3);
dispatch_async(myConcurrentqueue, blk4);
/*输出:不等待、完成顺序被打乱、多线程
2019-03-10 11:16:59.346941+0800 Block[97725:7855969] 非ui线程4 --><NSThread: 0x6000017fa600>{number = 5, name = (null)}
2019-03-10 11:16:59.346941+0800 Block[97725:7855971] 非ui线程1 --><NSThread: 0x6000017ff7c0>{number = 3, name = (null)}
2019-03-10 11:16:59.346941+0800 Block[97725:7855970] 非ui线程3 --><NSThread: 0x6000017fa5c0>{number = 4, name = (null)}
2019-03-10 11:16:59.347041+0800 Block[97725:7855968] 非ui线程2 --><NSThread: 0x6000017ff900>{number = 6, name = (null)}
2019-03-10 11:17:00.352596+0800 Block[97725:7855927] ui线程4
2019-03-10 11:17:00.352838+0800 Block[97725:7855927] ui线程3
2019-03-10 11:17:00.353006+0800 Block[97725:7855927] ui线程2
2019-03-10 11:17:00.353147+0800 Block[97725:7855927] ui线程1
*/
Main Dispatch Queue 和Global Dispatch Queue
名称 | 种类 | 说明 |
---|---|---|
Main Dispatch Queue | Serial Dispatch Queue | 主线程执行 |
Global Dispatch Queue(High Priority) | Concurrent Dispatch Queue | 执行优先级:高 |
Global Dispatch Queue(Default Priority) | Concurrent Dispatch Queue | 执行优先级:默认 |
Global Dispatch Queue(Low Priority) | Concurrent Dispatch Queue | 执行优先级:低 |
Global Dispatch Queue(Background Priority) | Concurrent Dispatch Queue | 执行优先级:后台 |
//获取高优先级Global Dispath Queue
dispatch_queue_t globalDispatchQueueHigh= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);