标签:
- (void)viewDidLoad
18 {
19 [super viewDidLoad];
20 //1.获得全局的并发队列
21 dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
22 //2.添加任务到队列中,就可以执行任务
23 //异步函数:具备开启新线程的能力
24 dispatch_async(queue, ^{
25 NSLog(@"下载图片1----%@",[NSThread currentThread]);
26 });
27 dispatch_async(queue, ^{
28 NSLog(@"下载图片2----%@",[NSThread currentThread]);
29 });
30 dispatch_async(queue, ^{
31 NSLog(@"下载图片2----%@",[NSThread currentThread]);
32 });
33 //打印主线程
34 NSLog(@"主线程----%@",[NSThread mainThread]);
35
36 }
总结:同时开启三个子线 - (void)viewDidLoad
18 {
19 [super viewDidLoad];
20
21 //打印主线程
22 NSLog(@"主线程----%@",[NSThread mainThread]);
23
24 //创建串行队列
25 dispatch_queue_t queue= dispatch_queue_create("wendingding", NULL);
26 //第一个参数为串行队列的名称,是c语言的字符串
27 //第二个参数为队列的属性,一般来说串行队列不需要赋值任何属性,所以通常传空值(NULL)
28
29 //2.添加任务到队列中执行
30 dispatch_async(queue, ^{
31 NSLog(@"下载图片1----%@",[NSThread currentThread]);
32 });
33 dispatch_async(queue, ^{
34 NSLog(@"下载图片2----%@",[NSThread currentThread]);
35 });
36 dispatch_async(queue, ^{
37 NSLog(@"下载图片2----%@",[NSThread currentThread]);
38 });
39
40
总结:会开启线程,但是只开启一个线程
- (void)viewDidLoad
20 {
21 [super viewDidLoad];
22
23 //打印主线程
24 NSLog(@"主线程----%@",[NSThread mainThread]);
25
26 //创建串行队列
27 dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
28
29
30 //2.添加任务到队列中执行
31 dispatch_sync(queue, ^{
32 NSLog(@"下载图片1----%@",[NSThread currentThread]);
33 });
34 dispatch_sync(queue, ^{
35 NSLog(@"下载图片2----%@",[NSThread currentThread]);
36 });
37 dispatch_sync(queue, ^{
38 NSLog(@"下载图片3----%@",[NSThread currentThread]);
39 });
40 }
41
总结:不会开启新的线程,并发队列失去了并发的功能
*用同步函数往串行队列中添加任务
20 */
21 - (void)viewDidLoad
22 {
23 [super viewDidLoad];
24 NSLog(@"用同步函数往串行队列中添加任务");
25 //打印主线程
26 NSLog(@"主线程----%@",[NSThread mainThread]);
27
28 //创建串行队列
29 dispatch_queue_t queue= dispatch_queue_create("wendingding", NULL);
30
31 //2.添加任务到队列中执行
32 dispatch_sync(queue, ^{
33 NSLog(@"下载图片1----%@",[NSThread currentThread]);
34 });
35 dispatch_sync(queue, ^{
36 NSLog(@"下载图片2----%@",[NSThread currentThread]);
37 });
38 dispatch_sync(queue, ^{
39 NSLog(@"下载图片3----%@",[NSThread currentThread]);
40 });
41 }
42
总结:不会开启新的线程
- (void)viewDidLoad
22 {
23 [super viewDidLoad];
24 NSLog(@"用同步函数往串行队列中添加任务");
25 //打印主线程
26 NSLog(@"主线程----%@",[NSThread mainThread]);
27
28 //创建串行队列
29 dispatch_queue_t queue= dispatch_queue_create("wendingding", NULL);
30
31 //2.添加任务到队列中执行
32 dispatch_sync(queue, ^{
33 NSLog(@"下载图片1----%@",[NSThread currentThread]);
34 });
35 dispatch_sync(queue, ^{
36 NSLog(@"下载图片2----%@",[NSThread currentThread]);
37 });
38 dispatch_sync(queue, ^{
39 NSLog(@"下载图片3----%@",[NSThread currentThread]);
40 });
41 }
总结:不会开启新的线程
标签:
原文地址:http://www.cnblogs.com/zs12/p/5081477.html