//异步函数+并发队列会开启多条线程,异步执行
- (void)asyncConcurrent{
//1.创建队列
//第一个参数:c语言字符串,就是一个标识符,用来区分队列
//第二个参数:队列的类型:DISPATCH_QUEUE_SERIAL串行DISPATCH_QUEUE_CONCURRENT并行
dispatch_queue_tqueue =dispatch_queue_create("first",DISPATCH_QUEUE_CONCURRENT);
//2.封装任务->添加任务到队列中
//第一个参数:队列
//第二参数:要执行的任务
dispatch_async(queue, ^{
NSLog(@"first====%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"second====%@",[NSThreadcurrentThread]);
});
dispatch_async(queue, ^{
NSLog(@"third====%@",[NSThreadcurrentThread]);
});
}
//异步函数+串行队列:会开线程,开一条线程,队列中的任务是串行执行的
-(void)asyncSerial
{
//1.创建队列
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]);
});
}
//同步函数+并发队列:不会开线程,任务是串行执行的
-(void)syncConcurrent
{
//1.创建队列
dispatch_queue_tqueue =dispatch_queue_create("download",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"---start---");
//2.封装任务
dispatch_sync(queue, ^{
NSLog(@"download1----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download2----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download3----%@",[NSThreadcurrentThread]);
});
NSLog(@"---end---");
}
//同步函数+串行队列:不会开线程,任务是串行执行的
-(void)syncSerial
{
//1.创建队列
dispatch_queue_tqueue =dispatch_queue_create("download",DISPATCH_QUEUE_SERIAL);
//2.封装任务
dispatch_sync(queue, ^{
NSLog(@"download1----%@",[NSThreadcurrentThread]);
});dispatch_sync(queue, ^{
NSLog(@"download2----%@",[NSThreadcurrentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"download3----%@",[NSThreadcurrentThread]);
});
//获得全局并发队列
/*
第一个参数:优先级
第二个参数:
*/
dispatch_queue_tqueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);