GCD10

并发队列:GCD默认提供了全局的并发队列,提供整个应用使用,可以无需手动创建

使用dispatch_get_global_queue函数获得全局的并发队列

dispatch_queue_t dispatch_get_global_queue()(

dispatch_queue_prioprity_t priority,//队列的优先级

unsigned long flags);//此参数暂时无用,用0即可

获得全局并发队列

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

串行队列

GCD中获得串行队列有2中途径

使用dispatch_queue_create函数创建串行队列

//创建串行队列(队列里欸选哪个传递NULL或者DISPATCH_QUEUE_SERIAL)

dispatch_queue_t queue  = dispatch_queue_create("com.queue",NULL);

使用主队列(跟线程相关联的队列)

放在主队列中的任务,都会放到主线程中执行

使用dispatch_get_main_queue()获得主队列

dispatch_queue_t queue = dispatch_get_main_queue();


//代码实现部分

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

[self asycConcurrent];

[self syncConcurrent];

[self asyncSerial];

[self syncSerial];


}

/*

同步函数 + 主队列(会发生阻塞)

*/

- (void)synMain{

NSLog(@"---------------begin");//只执行这一句

//获得主队列

dispatch_queue_t queue  = dispatch_get_main_queue();

//将任务添加到队列中

dispatch_sync(queue,^{

NSLog(@"1-----%@",[NSThread currentThread]);

});

dispatch_sync(queue,^{

NSLog(@"2-----%@",[NSThread currentThread]);

});

dispatch_sync(queue,^{

NSLog(@"3-----%@",[NSThread currentThread]);

});

NSLog(@"---------------end");

}

/*

异步函数 + 主队列:只在主线程中执行任务

*/

- (void)asynMain{

//获得主队列

dispatch_queue_t queue  = dispatch_get_main_queue();

//将任务添加到队列中

dispatch_async(queue,^{

NSLog(@"1-----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"2-----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"3-----%@",[NSThread currentThread]);

});

}

/*

同步函数+串行队列:不会开启新的线程,再当前线程执行任务,但是任务是串行的,执行完一个任务后,再执行下一个任务

*/

- (void)syncSerial{

//创建串行队列

dispatch_queue_t queue = dispatch_queue_create("com.queue",DISPATCH_QUEUE_SERIAL);

//2.将任务加入队列

dispatch_sync(queue,^{

NSLog(@"1------%@",[NSThread currentThread]);

});

dispatch_sync(queue,^{

NSLog(@"2------%@",[NSThread currentThread]);

});

dispatch_sync(queue,^{

NSLog(@"3------%@",[NSThread currentThread]);

});



}

/*

异步函数 + 串行队列:会开启新的线程,但是任务是串行的,执行完一个任务后,再执行下一个任务

*/

-(void)asyncSerial{

//1.创建串行队列

dispatch_queue_t queue = dispatch_queue_create("com.queue",DISPATCH_QUEUE_SERIAL);

//2.将任务加入队列

dispatch_async(queue,^{

NSLog(@"1-----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"2-----%@",[NSThread currentThread]);

});

dispatch_async(queue,^{

NSLog(@"3-----%@",[NSThread currentThread]);

});


}

/*

同步函数 + 并发队列:不会开启新的线程

*/

- (void)syncConcurrent{

//获得全局并发队列

dispatch_queue_t queue  = dispatch_get_global_queue(DISPATCH_QUUE_PRIORITY_DEFAULT,0);

dispatch_sync(queue,^{

NSLog(@"1-=======================%@",[NSThread currentThread]);//打印出来的是主线程

});


}

/**

异步函数 + 并发队列:可以同时开启多条函数

**/

-(void)asyncConcurrent{

dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT,0);

dispatch_async(queue,^{

for(NSInteger i = 0; i < 10; i++){

NSLog(@"1-=======================%@",[NSThread currentThread]);

}

});

dispatch_async(queue,^{

for(NSInteger i = 0; i < 10; i++){

NSLog(@"2-=======================%@",[NSThread currentThread]);

}

});

dispatch_async(queue,^{

for(NSInteger i = 0; i < 10; i++){

NSLog(@"3-=======================%@",[NSThread currentThread]);

}

});

}

你可能感兴趣的:(GCD10)