多线程编程(三种方式)
一、NSThread
DownloadFile是一个继承NSObject的类,当中的runDownload方法是需要放到线程中执行;ThreadDownload则是一个继承NSThread的子线程类,配合隐式创建线程的方式,执行threadD对象中的run:函数并传入字符串@"download3"
// download DownloadFile *download = [[DownloadFile alloc] init]; // 动态创建线程 NSThread *downloadThread = [[NSThread alloc] initWithTarget:download selector:@selector(runDownload:) object:@"download1"]; [downloadThread start]; // 暂停当前线程2秒 [NSThread sleepForTimeInterval:2]; // 静态方法创建线程 [NSThread detachNewThreadSelector:@selector(runDownload:) toTarget:download withObject:@"download2"]; // 隐式创建线程 ThreadDownload *threadD = [[ThreadDownload alloc] init]; [threadD performSelectorInBackground:@selector(run:) withObject:@"download3"]; [self performSelectorInBackground:@selector(mainRun:) withObject:@"download4"];
二、配合使用NSOperation和NSOperationQueue实现多线程编程
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; // 使用NSOperation的子类NSBlokOperation NSBlockOperation *blockOperation = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"blockOperation--------%@", [NSThread currentThread]); }]; blockOperation.completionBlock = ^() { NSLog(@"blockOperation 执行完毕!"); }; // [blockOperation start]; // [[NSOperationQueue mainQueue] addObject:blockOperation]; // 使用NSOperation的子类NSInvocationOperation NSInvocationOperation *invocationOperation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mainRun:) object:@"invocationOperation"]; invocationOperation.completionBlock = ^() { NSLog(@"invocationOperation 执行完毕!"); }; // 设置依赖关系,前者依赖于后者,当两个operation都放入queue中时,后者先运行于前者 [invocationOperation addDependency:blockOperation]; // 放入操作队列(NSOperationQueue)中,实现异步多线程操作 [queue addOperation:blockOperation]; // 暂停queue 2秒后 恢复queue [queue setSuspended:YES]; [NSThread sleepForTimeInterval:2.0]; [queue setSuspended:NO]; [queue addOperation:invocationOperation];
三、GCD线程管理(推荐)
// GCD并行操作 任务并发执行 // 并行可不需显示创建,每次都直接调用dispatch_get_global_queue获得queue就行了 dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(concurrentQueue, ^{ NSLog(@"GCD并行异步任务1------%@", [NSThread currentThread]); }); dispatch_async(concurrentQueue, ^{ NSLog(@"GCD并行异步任务2------%@", [NSThread currentThread]); }); // GCD串行操作 会按照添加的任务顺序,先进先出,前一个任务完成才执行下一个 // 串行需要显示创建,可以创建多个串行队列,串行队列也可以并行执行,但决不能随意的大量创建串行队列 dispatch_queue_t serialQueue = dispatch_queue_create("our_serial_queue", NULL); dispatch_async(serialQueue, ^{ NSLog(@"GCD串行异步任务1------%@", [NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"GCD串行异步任务2------%@", [NSThread currentThread]); }); // Dispatch Group的使用 -> 并行执行多个任务,然后等待全部任务完成再回到主线程执行任务 [NSThread sleepForTimeInterval:2.0]; NSLog(@"******************************************GCD**********************************************"); dispatch_async(concurrentQueue, ^{ dispatch_group_t group = dispatch_group_create(); // 往group添加并行异步任务 dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"并发组异步任务1-------%@", [NSThread currentThread]); }); dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"并发组异步任务2-------%@", [NSThread currentThread]); }); // 等待组中的任务执行完毕,回到主线程执行block回调 dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"Dispatch Group主线程任务回调!--------%@", [NSThread currentThread]); }); });
参考链接:http://www.cnblogs.com/mjios/archive/2013/04/18/3029309.html