GCD汇总
//总结如下: //1.同步请求:不会开启新的线程 //1-1.同步请求--串行队列:不开启新线程--按照顺序执行下去 //1-2.同步请求--并发列队:不开启新线程--按照顺序执行下去 //2.异步请求:会开启新的线程 //2-1.异步请求--串行队列:开启一条新线程--按照顺序执行下去 //2-2.异步请求--并发队列:每条请求开启一条新线程--无顺序执行 //3.主线程 //主线程上面的请求都会在主线程上面执行 //3-1.异步请求:在主线程上面不会开启新的线程--按照顺序执行下去 //3-2.同步请求:卡死 当触发同步请求的时候会立即触发这个方法,但是上面的方法还需要往下面走,相互之间卡死了。 - (void)viewDidLoad { [super viewDidLoad]; [self test8]; } // <NSThread: 0x7fd2c3c088a0>{number = 1, name = main} // 2><NSThread: 0x7fd2c3c14280>{number = 2, name = (null)} // 4><NSThread: 0x7fd2c3c9a6a0>{number = 4, name = (null)} // 3><NSThread: 0x7fd2c3e0c4b0>{number = 3, name = (null)} // 1><NSThread: 0x7fd2c3e0d570>{number = 5, name = (null)} // 5><NSThread: 0x7fd2c3c14280>{number = 2, name = (null)} //异步队列--异步请求 //会创建新的线程--线程之间的执行时是并发的 -(void)test1{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(queue, ^{ NSLog(@"1>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"2>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"3>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"4>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"5>%@",[NSThread currentThread]); }); } //结果如下 // <NSThread: 0x7fd5bac04910>{number = 1, name = main} // 1><NSThread: 0x7fd5bac04910>{number = 1, name = main} // 2><NSThread: 0x7fd5bac04910>{number = 1, name = main} // 3><NSThread: 0x7fd5bac04910>{number = 1, name = main} // 4><NSThread: 0x7fd5bac04910>{number = 1, name = main} // 5><NSThread: 0x7fd5bac04910>{number = 1, name = main} //异步队列--同步请求 //并没有开启新的线程--还是在主线程上面串行的执行 -(void)test2{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_sync(queue, ^{ NSLog(@"1>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"2>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"3>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"4>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"5>%@",[NSThread currentThread]); }); } //结果如下 // <NSThread: 0x7fed50d07b50>{number = 1, name = main} // 1><NSThread: 0x7fed50d07b50>{number = 1, name = main} // 2><NSThread: 0x7fed50d07b50>{number = 1, name = main} // 3><NSThread: 0x7fed50d07b50>{number = 1, name = main} // 4><NSThread: 0x7fed50d07b50>{number = 1, name = main} // 5><NSThread: 0x7fed50d07b50>{number = 1, name = main} //串行队列--同步请求 //并没有开启新的线程--还是在主线程上面串行的执行 -(void)test3{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL); dispatch_sync(queue, ^{ NSLog(@"1>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"2>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"3>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"4>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"5>%@",[NSThread currentThread]); }); } //结果如下 // <NSThread: 0x7faa8a606430>{number = 1, name = main} // 1><NSThread: 0x7faa8a61e880>{number = 2, name = (null)} // 2><NSThread: 0x7faa8a61e880>{number = 2, name = (null)} // 3><NSThread: 0x7faa8a61e880>{number = 2, name = (null)} // 4><NSThread: 0x7faa8a61e880>{number = 2, name = (null)} // 5><NSThread: 0x7faa8a61e880>{number = 2, name = (null)} //串行队列--异步请求 //开启了一条新的线程--按照顺序执行 -(void)test4{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ NSLog(@"1>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"2>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"3>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"4>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"5>%@",[NSThread currentThread]); }); } //结果如下 // <NSThread: 0x7fe849c048f0>{number = 1, name = main} // 1><NSThread: 0x7fe849c048f0>{number = 1, name = main} // 2><NSThread: 0x7fe849c048f0>{number = 1, name = main} // 3><NSThread: 0x7fe849c048f0>{number = 1, name = main} // 4><NSThread: 0x7fe849c048f0>{number = 1, name = main} // 5><NSThread: 0x7fe849c048f0>{number = 1, name = main} //主线程--异步请求 //不会开启新的线程,都是在主线程上面执行的 -(void)test5{ NSLog(@"%@",[NSThread currentThread]); 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]); }); dispatch_async(queue, ^{ NSLog(@"4>%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"5>%@",[NSThread currentThread]); }); } //结果如下 // <NSThread: 0x7fe881501cc0>{number = 1, name = main} //主线程--同步请求 //卡主线程 -(void)test6{ NSLog(@"%@",[NSThread currentThread]); 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]); }); dispatch_sync(queue, ^{ NSLog(@"4>%@",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"5>%@",[NSThread currentThread]); }); } //结果如下 // <NSThread: 0x7fc70a7023c0>{number = 1, name = main} // 2><NSThread: 0x7fc70a443ac0>{number = 2, name = (null)} // 3><NSThread: 0x7fc70a61a740>{number = 4, name = (null)} // 1><NSThread: 0x7fc70a70ea60>{number = 3, name = (null)} // done //开启多条线程 //统一完成后 -(void)test7{ NSLog(@"%@",[NSThread currentThread]); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ NSLog(@"1>%@",[NSThread currentThread]); }); dispatch_group_async(group, queue, ^{ NSLog(@"2>%@",[NSThread currentThread]); }); dispatch_group_async(group, queue, ^{ NSLog(@"3>%@",[NSThread currentThread]); }); dispatch_group_notify(group, queue, ^{ NSLog(@"done"); }); }