NSThread
-(void)commonThread { /** 使用NSThread感觉不方便的地方就是每次使用线程都得创建一个新的方法。不过它的方法好记,简单易用,只是功能上有点弱。 */ //NSThread的两种使用 //1. NSThread *thread1 = [[NSThread alloc]initWithTarget:self selector:@selector(readNUm1) object:nil]; [thread1 start]; //2. [NSThread detachNewThreadSelector:@selector(loadThreadArgumentWithStr:) toTarget:self withObject:@"argument"]; //回到主线程中做一些事情(UI只能在主线程中刷新,当然干点别的肯定也是可以的) [self performSelectorOnMainThread:@selector(readNum2) withObject:nil waitUntilDone:NO]; //NSObject - 对象的某个方法可以直接在后台运行 [self performSelectorInBackground:@selector(loadThreadArgumentWithStr:) withObject:@"NSobjectThread"]; }
NSOperation&NSOperationQueue
//NSOperationQueue会建立一个线程管理器,每个加入到线程operation会有序的执行。 -(void)loadOperation { /** [XXXXX new] ~= [[XXXX alloc]init] new是隐式调用了init,如果使用alloc/init 我们可以定制initXXX */ NSOperationQueue *queue = [NSOperationQueue new]; //设置线程最大并发数 [queue setMaxConcurrentOperationCount:2]; NSInvocationOperation *operation =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(threadReadNums) object:nil]; NSInvocationOperation *operation2 =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(threadReadNums2) object:nil]; NSInvocationOperation *operation3 =[[NSInvocationOperation alloc] initWithTarget:self selector:@selector(threadReadNums) object:nil]; [queue addOperation:operation]; [queue addOperation:operation2]; [queue addOperation:operation3]; NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{ for (int i=0; i<10; i ++) { NSLog(@"NSBlockOperation -->%zi",i); } }]; [queue addOperation:blockOperation]; }
添加依赖关系
-(void)AddPriorityOperation { NSOperationQueue *queue = [NSOperationQueue new]; NSBlockOperation *blockOperation1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockOperation 1"); }]; NSBlockOperation *blockOperation2 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockOperation 2"); }]; NSBlockOperation *blockOperation3 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockOperation 3"); }]; NSBlockOperation *blockOperation4 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockOperation 4"); }]; NSBlockOperation *blockOperation5 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockOperation 5"); }]; NSBlockOperation *blockOperation6 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockOperation 6"); }]; [blockOperation6 setQueuePriority:NSOperationQueuePriorityHigh]; [blockOperation6 addDependency:blockOperation1];//依赖关系优于优先级的设定 [blockOperation1 addDependency:blockOperation2]; [blockOperation2 addDependency:blockOperation3]; [blockOperation3 addDependency:blockOperation4]; [blockOperation4 addDependency:blockOperation5]; [queue setMaxConcurrentOperationCount:6]; [queue addOperation:blockOperation1]; [queue addOperation:blockOperation2]; [queue addOperation:blockOperation3]; [queue addOperation:blockOperation4]; [queue addOperation:blockOperation5]; [queue addOperation:blockOperation6]; }
GCD的使用
1 -(void)dispatchMethods 2 { 3 //1.后台执行 4 dispatch_async(dispatch_get_global_queue(0, 0), ^{ 5 //dosomething 6 }); 7 //2.主线程执行 8 dispatch_async(dispatch_get_main_queue(), ^{ 9 //dosomething 10 }); 11 //3.一次执行 12 static dispatch_once_t onceToken; 13 dispatch_once(&onceToken, ^{ 14 //代码会被执行一次 15 }); 16 //4.延时执行 17 dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC); 18 dispatch_after(time, dispatch_get_main_queue(), ^(void){ 19 //2秒后执行 20 }); 21 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ 22 //2秒后执行 23 }); 24 //5.自定义方式 使用idspatch_queue_create方法 25 dispatch_queue_t urls_queue = dispatch_queue_create("url", NULL); 26 dispatch_async(urls_queue, ^{ 27 //执行代码 28 }); 29 //6.在后台并行执行多个线程,并在执行完后汇总执行结果, 30 //用dispatch_group,dispatch_async和dicpatch_group_notify实现 31 dispatch_group_t group = dispatch_group_create(); 32 dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ 33 //线程任务一 34 }); 35 dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ 36 //线程任务二 37 }); 38 dispatch_group_notify(group, dispatch_get_global_queue(0, 0), ^{ 39 //汇总结果。。。在所有group中的线程任务都完成后会执行该方法 40 }); 41 }