一、多线程的理解
1.多线程可以使多个线程并行的工作以完成多项任务,以提高系统的效率。线程是在同一时间需要完成多项任务的时候被实现的。
2.在刷新tableView,Block回调这都必须回到主线程。
二、多线程的种类
1.NSThread
2.NSOperation和NSOperationQueue
3.GCD(Grand Central Dispatch)
三、多线程的操作
1.NSThread
//NSTread 线程手动启动1 - (IBAction)clickNSTreadBtnAction:(UIButton *)sender { NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(addWithStr:) object:@"dsbhdbj"]; [thread start]; } //NSTread 线程手动启动2 - (IBAction)clickNSTreadBtnAction2:(UIButton *)sender { [NSThread detachNewThreadSelector:@selector(addWithStr:) toTarget:self withObject:@"张亮真帅"]; } //求和 -(void)addWithStr:(NSString *)str{ NSLog(@"%@", str); int sum = 0; for (int i = 0; i < 900000000; i++) { sum += i; NSLog(@"%d",sum); } }
2.NSOperation和NSOperationQueue
//NSOperation的子类:NSInvocationOperation和NSBlockOperation //注意:当NSOperation的子类关联执行方法或者block语法块,单一使用的只是执行了关联的方法或者block语法块,并没有开子线程,如果需要开子线程执行,需要和NSOperationQueue联合使用 //方法一 NSInvocationOperationn - (IBAction)clickOperationBtnAction:(UIButton *)sender { NSInvocationOperation *operation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(addWithStr:) object:@"sdjd"]; //执行 [operation start]; } //方法二 NSBlockOperation - (IBAction)clickBlockOperationBtnAction:(UIButton *)sender { NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{ [self addWithStr:nil]; }]; //执行 [blockOperation start]; } //OperationQueue - (IBAction)clickOperationQueueBtnAction:(UIButton *)sender { //创建Operation 对象 NSInvocationOperation *operation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(operation1) object:nil]; //创建Operation 对象 NSBlockOperation *operation2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"我是2"); }]; //创建OperationQueue对象 1和2 是并发执行的 无序 NSOperationQueue *queue = [[NSOperationQueue alloc]init]; //最大并发数 queue.maxConcurrentOperationCount = 1; //依赖关系 2先执行 1后执行 [operation1 addDependency:operation2]; //添加OperationQueue对象,(Operation 不需要手动开启) [queue addOperation:operation2]; [queue addOperation:operation1]; } -(void)operation1{ NSLog(@"我是1"); }
3.GCD(Grand Central Dispatch)
1.GCD主队列
- (IBAction)clickGCDBtnAction:(UIButton *)sender { //主队列(串行) 按顺序执行 //获取主队列 dispatch_queue_t mainQueue = dispatch_get_main_queue(); //执行任务 第一个参数:队列 第二个参数:任务 dispatch_async(mainQueue, ^{ NSLog(@"我是第一个"); }); dispatch_async(mainQueue, ^{ NSLog(@"我是第二个"); }); dispatch_async(mainQueue, ^{ for (int i = 0; i < 10; i++) { NSLog(@"%d",++i); } }); dispatch_async(mainQueue, ^{ NSLog(@"我是第四个"); }); dispatch_async(mainQueue, ^{ NSLog(@"我是第五个"); }); }
2.GCD自定义队列
- (IBAction)clickGCDBtnAction:(UIButton *)sender { //自定义队列(串行的、并发的) //第一个参数: 队列名 第二个参数:属性(默认DISPATH_QUEUE_SERIAL(NULL)代表串行 DISPATCH_QUEUE_CONCURRENT代表并发) //获取主队列 dispatch_queue_t queue1 = dispatch_queue_create("queue1", DISPATCH_QUEUE_CONCURRENT); //给队列添加任务 dispatch_async(queue1, ^{ NSLog(@"queue1-1"); }); dispatch_async(queue1, ^{ NSLog(@"queue1-2"); }); dispatch_async(queue1, ^{ NSLog(@"queue1-3"); }); dispatch_async(queue1, ^{ NSLog(@"queue1-4"); }); }
3.GCD全局队列
- (IBAction)clickGCDBtnAction:(UIButton *)sender { //全局队列(并发的) //获取主队列 //第一个参数:(优先级别) 第二个参数:标志 dispatch_queue_t global = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0); dispatch_queue_t global2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); dispatch_async(global2, ^{ NSLog(@"global2-1"); }); dispatch_async(global2, ^{ NSLog(@"global2-2"); }); dispatch_async(global2, ^{ NSLog(@"global2-3"); }); dispatch_async(global2, ^{ NSLog(@"global2-4"); }); dispatch_async(global, ^{ NSLog(@"global-1"); }); dispatch_async(global, ^{ NSLog(@"global-2"); }); dispatch_async(global, ^{ NSLog(@"global-3"); }); dispatch_async(global, ^{ NSLog(@"global-4"); }); }
四、GCD官方文档
https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/doc/uid/TP40008079