//異步函數(shù)+并發(fā)隊列會開啟多條線程,異步執(zhí)行
- (void)asyncConcurrent{
//1.創(chuàng)建隊列
//第一個參數(shù):c語言字符串,就是一個標識符,用來區(qū)分隊列
//第二個參數(shù):隊列的類型:DISPATCH_QUEUE_SERIAL串行DISPATCH_QUEUE_CONCURRENT并行
dispatch_queue_tqueue =dispatch_queue_create("first",DISPATCH_QUEUE_CONCURRENT);
//2.封裝任務(wù)->添加任務(wù)到隊列中
//第一個參數(shù):隊列
//第二參數(shù):要執(zhí)行的任務(wù)
dispatch_async(queue, ^{
NSLog(@"first====%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"second====%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"third====%@",[NSThreadcurrentThread]);
});
}
//異步函數(shù)+串行隊列:會開線程,開一條線程,隊列中的任務(wù)是串行執(zhí)行的
-(void)asyncSerial
{
//1.創(chuàng)建隊列
dispatch_queue_tqueue =dispatch_queue_create("download",DISPATCH_QUEUE_SERIAL);
//2.封裝操作
dispatch_async(queue, ^{
NSLog(@"download1----%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download2----%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3----%@",[NSThreadcurrentThread]);
});
}
//同步函數(shù)+并發(fā)隊列:不會開線程,任務(wù)是串行執(zhí)行的
-(void)syncConcurrent
{
//1.創(chuàng)建隊列
dispatch_queue_tqueue =dispatch_queue_create("download",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---start---");
//2.封裝任務(wù)
dispatch_sync(queue, ^{
NSLog(@"download1----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download2----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download3----%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
}
//同步函數(shù)+串行隊列:不會開線程,任務(wù)是串行執(zhí)行的
-(void)syncSerial
{
//1.創(chuàng)建隊列
dispatch_queue_tqueue =dispatch_queue_create("download",DISPATCH_QUEUE_SERIAL);
//2.封裝任務(wù)
dispatch_sync(queue, ^{
NSLog(@"download1----%@",[NSThreadcurrentThread]);
});dispatch_sync(queue, ^{
NSLog(@"download2----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download3----%@",[NSThreadcurrentThread]);
});
//獲得全局并發(fā)隊列
/*
第一個參數(shù):優(yōu)先級
第二個參數(shù):
*/
dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);