iOS开发中遇到的几种多线程
2015-02-12 16:01
113 查看
最近整理了一下iOS开发中常用的几种多线程
// 第五种方式
线程队列可以同时添加多个线程 可以设置优先级等
// 第六种方式 GCD 这个性能最好 推荐使用这个
// 多线程执行的方法
注意!这边需要有一个自动释放池
// 主线程
// 第一种方式
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(mutableThread) object:@"test"]; [thread start];
// 第二种方式
[NSThread detachNewThreadSelector:@selector(mutableThread) toTarget:self withObject:nil];
// 第三种方式
[self performSelectorInBackground:@selector(mutableThread) withObject:nil];
// 第四种方式
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init]; [operationQueue addOperationWithBlock:^{ // 多线程上执行的方法 }];
// 第五种方式
线程队列可以同时添加多个线程 可以设置优先级等
NSOperationQueue *threadQueue = [[NSOperationQueue alloc] init]; NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread) object:nil]; NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(mutableThread) object:nil]; [threadQueue addOperation:op1]; [threadQueue addOperation:op2];
// 第六种方式 GCD 这个性能最好 推荐使用这个
dispatch_queue_create("test", NULL); dispatch_async(queue, ^{ // 多线程 // 回到主线程执行 dispatch_sync(dispatch_get_main_queue(), ^{ 这里写代码片// 主线程操作的代码 }); }); return YES; }
// 多线程执行的方法
注意!这边需要有一个自动释放池
- (void)mutableThread { @autoreleasepool { [self performSelectorOnMainThread:@selector(mainThread) withObject:nil waitUntilDone:NO]; } }
// 主线程
- (void)mianThread { }
相关文章推荐
- iOS 开发中几种多线程方案
- iOS 开发中几种多线程方案
- 【iOS开发-多线程】多线程创建的几种方法对比
- 谈谈你对多线程开发的理解?ios中有几种实现多线程的方法?
- OSATOMIC.h的介绍 OSATOMIC与synchronized 加锁的对比 iOS开发 多线程编程
- PhoneGap 2.0 iOS开发环境搭建可能遇到的issue
- 在ios开发中遇到数据最好采用c语言中的数组存储
- PhoneGap 2.0 iOS开发环境搭建可能遇到的issue
- iOS开发肯定会遇到的
- SQLite的多线程应用与iOS开发
- 【无限互联】iOS开发之多线程开发
- ios 开发中遇到 UIAlertView 窗口无法release的问题
- 当C++遇到IOS应用开发---Dict集合
- 当C++遇到iOS应用开发之---List集合
- 当C++遇到IOS应用开发之---List集合
- 当C++遇到iOS应用开发---SQLITE篇
- ios一些开发中遇到的小知识点
- ios开发笔记之 多线程编程指南
- ios多线程开发——nsoperation详解
- 当C++遇到IOS应用开发---字符串处理