主線程:
dispatch_async(dispatch_get_main_queue(), ^{
});
子線程:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
子線程(Global Dispatch Queue)有四個(gè)優(yōu)先級(jí):
- 高優(yōu)先級(jí)(High Priority)
- 默認(rèn)優(yōu)先級(jí)(Default Priorty)
- 低優(yōu)先級(jí)(Low Priority)
- 后臺(tái)優(yōu)先級(jí)(Background Priority)
串行隊(duì)列線程組:
dispatch_queue_t queueT = dispatch_queue_create("group.queue", DISPATCH_QUEUE_CONCURRENT);//一個(gè)并發(fā)隊(duì)列
dispatch_group_t grpupT = dispatch_group_create();//一個(gè)線程組
dispatch_group_async(grpupT, queueT, ^{
NSLog(@"group——當(dāng)前線程一");
//模仿網(wǎng)絡(luò)請(qǐng)求代碼
dispatch_group_enter(grpupT);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 10; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"網(wǎng)絡(luò)圖片請(qǐng)求中 ···%d", i);
}
dispatch_group_leave(grpupT);
});
});
dispatch_group_async(grpupT, queueT, ^{
NSLog(@"group——當(dāng)前線程二");
//模仿網(wǎng)絡(luò)請(qǐng)求代碼
dispatch_group_enter(grpupT);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 10; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"網(wǎng)絡(luò)圖片2請(qǐng)求中 ···2_%d", i);
}
dispatch_group_leave(grpupT);
});
});
dispatch_group_async(grpupT, queueT, ^{
NSLog(@"group——當(dāng)前線程三");
//模仿網(wǎng)絡(luò)請(qǐng)求代碼
dispatch_group_enter(grpupT);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
for (int i = 0; i < 10; i++) {
[NSThread sleepForTimeInterval:1];
NSLog(@"網(wǎng)絡(luò)圖片3請(qǐng)求中 ···3_%d", i);
}
dispatch_group_leave(grpupT);
});
});
dispatch_group_notify(grpupT, queueT, ^{
NSLog(@"此時(shí)還是在子線程中");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"回到主線程");
});
});
希望可以幫助大家
如果哪里有什么不對(duì)或者不足的地方,還望讀者多多提意見或建議
iOS技術(shù)交流群:668562416