dispatch_after
dispatch_after延遲將任務(wù)提交到隊(duì)列中蝎亚,注意不是事先將任務(wù)添加到隊(duì)列中然后再延遲執(zhí)行澄暮。
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(serialQueue, ^{
NSLog(@"任務(wù)1名段,%@", [NSThread currentThread]);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5*NSEC_PER_SEC)), serialQueue, ^{
NSLog(@"5秒后提交該block,%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"任務(wù)2,%@", [NSThread currentThread]);
});
/**
dispatch_time_t
dispatch_time()創(chuàng)建時(shí)間
DISPATCH_TIME_NOW從現(xiàn)在開始計(jì)算
NSEC_PER_SEC=1秒
*/
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC));
dispatch_after(time, serialQueue, ^{
NSLog(@"2秒后提交該block赏寇,%@", [NSThread currentThread]);
});
從打印日志可以看出吉嫩,所有任務(wù)都是添加到串行隊(duì)列中的,由串行隊(duì)列的特性可知:先進(jìn)入對的任務(wù)結(jié)束嗅定,才會(huì)執(zhí)行后面的任務(wù)自娩,可驗(yàn)證dispatch_after是將任務(wù)延時(shí)添加到隊(duì)列中。
DISPATCH_TIME_NOW是指執(zhí)行到該行代碼時(shí)開始計(jì)算渠退。
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_sync(serialQueue, ^{
NSLog(@"任務(wù)1忙迁,%@", [NSThread currentThread]);
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5*NSEC_PER_SEC)), serialQueue, ^{
NSLog(@"5秒后提交該block,%@", [NSThread currentThread]);
});
dispatch_sync(serialQueue, ^{
NSLog(@"任務(wù)2,%@", [NSThread currentThread]);
sleep(10);
});
/**
dispatch_time_t
dispatch_time()創(chuàng)建時(shí)間
DISPATCH_TIME_NOW從現(xiàn)在開始計(jì)算
NSEC_PER_SEC=1秒
*/
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC));
dispatch_after(time, serialQueue, ^{
NSLog(@"2秒后提交該block碎乃,%@", [NSThread currentThread]);
});
從兩次的打印日志中也可以看出姊扔,dispatch_after是另外開辟了一條線程執(zhí)行的。
dispatch_group_t
dispatch_group_t:調(diào)度組梅誓,可以將隊(duì)列中的任務(wù)放到調(diào)度組中恰梢,由調(diào)度組來控制任務(wù)的執(zhí)行佛南。
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, concurrentQueue, ^{
NSLog(@"任務(wù)1%@", [NSThread currentThread]);
});
dispatch_group_async(group, concurrentQueue, ^{
NSLog(@"任務(wù)2%@", [NSThread currentThread]);
});
dispatch_group_async(group, concurrentQueue, ^{
NSLog(@"任務(wù)3%@", [NSThread currentThread]);
});
dispatch_group_notify(group, concurrentQueue, ^{
NSLog(@"調(diào)度組中concurrentQueue隊(duì)列的所有任務(wù)執(zhí)行完畢%@", [NSThread currentThread]);
});
- dispatch_group_async:創(chuàng)建一個(gè)異步任務(wù)將其放在concurrentQueue隊(duì)列中,并關(guān)聯(lián)到調(diào)度組中嵌言。
- dispatch_group_notify:concurrentQueue隊(duì)列中所有關(guān)聯(lián)到調(diào)度組中的任務(wù)結(jié)束時(shí)執(zhí)行嗅回。
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_enter(group);
dispatch_async(concurrentQueue, ^{
NSLog(@"任務(wù)1%@", [NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(concurrentQueue, ^{
NSLog(@"任務(wù)2%@", [NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(concurrentQueue, ^{
NSLog(@"任務(wù)3%@", [NSThread currentThread]);
dispatch_group_leave(group);
});
- dispatch_group_enter:任務(wù)入組。
- dispatch_group_leave:任務(wù)出組摧茴。
dispatch_group_enter和dispatch_group_leave需要成對出現(xiàn)绵载。
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3*NSEC_PER_SEC));
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_async(concurrentQueue, ^{
NSLog(@"任務(wù)1%@", [NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(concurrentQueue, ^{
NSLog(@"任務(wù)2%@", [NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(concurrentQueue, ^{
sleep(5);
NSLog(@"任務(wù)3%@", [NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_notify(group, concurrentQueue, ^{
NSLog(@"調(diào)度組中concurrentQueue隊(duì)列的所有任務(wù)執(zhí)行完畢%@", [NSThread currentThread]);
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_group_wait(group, time);
NSLog(@"頂多等你三秒,調(diào)度組不結(jié)束我就干自己的事去了");
});
dispatch_group_wait:在指定時(shí)間或調(diào)度組結(jié)束時(shí)苛白,執(zhí)行下面的任務(wù)娃豹。可以應(yīng)用在網(wǎng)絡(luò)請求時(shí)間較長時(shí)的處理购裙。
dispatch_apply
dispatch_apply:重復(fù)提交任務(wù)到隊(duì)列中懂版。
dispatch_apply(10, serialQueue, ^(size_t i) {
NSLog(@"在線程中循環(huán)10次執(zhí)行該block%ld%@", i, [NSThread currentThread]);
});
在當(dāng)前線程中執(zhí)行,不會(huì)開辟新的線程缓窜。
dispatch_apply(10, concurrentQueue, ^(size_t i) {
NSLog(@"在線程中循環(huán)10次執(zhí)行該block%ld%@", i, [NSThread currentThread]);
});
會(huì)開辟新的線程定续,異步執(zhí)行任務(wù)。
其他
- dispatch_queue_get_label:獲取隊(duì)列的名字禾锤。
//獲取指定隊(duì)列的名字
NSLog(@"%s", dispatch_queue_get_label(serialQueue));
//獲取當(dāng)前隊(duì)列的名字
NSLog(@"%s", dispatch_queue_get_label(DISPATCH_CURRENT_QUEUE_LABEL));