主隊列是GCD自帶的一種特殊串行隊列。放到主隊列的任務都會放到放到主線程執(zhí)行户誓。
主隊列特點:如果主隊列發(fā)現(xiàn)當前主線程有任務在執(zhí)行饼灿,那么主隊列會暫停調(diào)用隊列的任務,直到主線程空閑為止帝美。
//異步函數(shù)+主隊列:所有任務都在主線程中執(zhí)行,不會開線程
-(void)asyncMain
{
//1.獲得主隊列
dispatch_queue_tqueue =dispatch_get_main_queue();
//2.異步函數(shù)
dispatch_async(queue, ^{
NSLog(@"download1----%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download2----%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3----%@",[NSThreadcurrentThread]);
});
}
//同步函數(shù)+主隊列:死鎖
//注意:如果該方法在子線程中執(zhí)行,那么所有的任務在主線程中執(zhí)行,
-(void)syncMain
{
//1.獲得主隊列
dispatch_queue_tqueue =dispatch_get_main_queue();
NSLog(@"start----");
//2.同步函數(shù)
//同步函數(shù):立刻馬上執(zhí)行,如果我沒有執(zhí)行完畢,那么后面的也別想執(zhí)行
//異步函數(shù):如果我沒有執(zhí)行完畢,那么后面的也可以執(zhí)行
dispatch_sync(queue, ^{
NSLog(@"download1----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download2----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download3----%@",[NSThreadcurrentThread]);
});
NSLog(@"end---");
}
解決辦法:
[NSThread detachNewThreadSelector:@selector(syncMain) toTarget:self withObject:nil];