程序中需要有并行的操作,就会用到多线程
1、
[self performSelectorInBackground:@selector(task) withObject:nil];
2、NSThread代表执行的线程,可以使用该类的对象封装线程操作,可以使用该类创建、管理线程
a、使用类方法detachNewThreadSelector: toTarget: withObject:创建新的线程
[NSThread detachNewThreadSelector:@selector(task) toTarget:self withObject:nil];
b、创建NSTheard对象。
NSThread *thr = [[NSThread alloc]initWithTarget:self selector:@selector(task) object:nil];
[thr start];
c、自定义Thread对象
@interface myThread:NSThread
@implementation myThread
-(void)main
{
//在此添加线程方法
}
myThread *_thread = [[myThread alloc]init];
[myThread start];
3、NSOperationQueue:作为Operation的管理者,OperationQueue负责执行放入其中的所有操作对象
- (IBAction)Caculate:(id)sender
{
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue setMaxConcurrentOperationCount:1];//指定能够同时并发执行的操作个数
NSInvocationOperation *op1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(xiazai1) object:nil];
NSInvocationOperation *op2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(xiazai2) object:nil];
NSInvocationOperation *op3 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(xiazai3) object:nil];
[queue addOperation:op1];//将创建好的operation加入运行队列中
[queue addOperation:op2];
[queue addOperation:op3];
}
//从网上下载图片
-(void)xiazai1
{
NSData *data1 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.qqskycn.net/bq/bq_tp/200904/20090404193057343.gif"]];
NSLog(@"data1 = %@",data1);
Image1.image = [UIImage imageWithData:data1];
}
-(void)xiazai2
{
NSData *data2 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://imgqq.5308.com/20061107/uploadfile/2005/8/11/14511674586.jpg"]];
Image2.image = [UIImage imageWithData:data2];
}
-(void)xiazai3
{
NSData *data3 = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://img1.comic.zongheng.com/comic/image/2009/1/samosekayi/ori/20090217025931686184.jpg"]];
Image3.image = [UIImage imageWithData:data3];
}
4、
GCD是和block紧密相连的,所以最好先了解下block(可以查看这里).GCD是C level的函数,这意味着它也提供了C的函数指针作为参数,方便了C程序员.
下面首先来看GCD的使用:
dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
async表明异步运行,block代表的是你要做的事情,queue则是你把任务交给谁来处理了.(除了async,还有sync,delay,本文以async为例).
之所以程序中会用到多线程是因为程序往往会需要读取数据,然后更新UI.为了良好的用户体验,读取数据的操作会倾向于在后台运行,这样以避免阻塞主线程.GCD里就有三种queue来处理.
1. Main queue:
顾名思义,运行在主线程,由dispatch_get_main_queue获得.和ui相关的就要使用Main Queue.
2.Serial quque(private dispatch queue)
每次运行一个任务,可以添加多个,执行次序FIFO. 通常是指程序员生成的,比如:
NSDate *da = [NSDate date];
NSString *daStr = [da description];
const char *queueName = [daStr UTF8String];
dispatch_queue_t myQueue = dispatch_queue_create(queueName, NULL);
3.Concurrent queue(global dispatch queue):
可以同时运行多个任务,每个任务的启动时间是按照加入queue的顺序,结束的顺序依赖各自的任务.使用dispatch_get_global_queue获得.
所以我们可以大致了解使用GCD的框架:
dispatch_async(getDataQueue,^{
//获取数据,获得一组后,刷新UI.
dispatch_aysnc (mainQueue, ^{
//UI的更新需在主线程中进行
});
});
分享到:
相关推荐
非常好的ios开发之多线程的资料 自愿下载原则
iOS开发多线程,NSoperetion, GCD,多线程的网络图片的下载
iOS开发学习之iOS多线程和RunLoop.pdf
ios多线程开发的常用四种方式和基本使用,pthread、NSThread、NSOperation\NSOperationQueue、GCD
iOS 开发多线程指南,很好的了解多线程开发
ios开发_iOS开发实用demo之多图片多线程下载实例
ios多线程开发
两个很精典的IOS中使用多线程的代码。有关NSThread类的使用一看这两个例子马上就能理解了。
移动应用开发_iOS开发示例教程之多图片多线程下载
iOS多线程开发一 使用NSThread NSThread的基本使用 // demo说明 NSThreadDemoOne: 简单使用多线程,区分有多线程和没有多线的区别 NSThreadDemoTwo: 因为NSThread只能传一个一个参数,如果咬传递多个参数,使用封装...
iphone开发中,一个利用多线程(序列)下载图片的例子。
深入介绍IOS内存管理和多线程操作,有助于提高ios开发水命
这套资料共分为"语法","常识","界面","iOS","操作","数据结构","多线程","网络","多媒体","安全"十个篇章。 大家可以利用这些试题加强iOS开发的理论知识,当然对面试也是很有帮助的! 试题参考答案在每篇的最后一页...
iOS多线程开发NSThread.pdf
IOS实例开发源码——多线程编程.zip
分为这套资料共分为"语法","常识","界面","iOS","操作","数据结构","多线程","网络","多媒体","安全"十个篇章。 大家可以利用这些试题加强iOS开发的理论知识,当然对面试也是很有帮助的! 试题参考答案在每篇的最后...
配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOperationQueue实现多线程的具体步骤 先将需要执行的操作封装到一个NSOperation对象中 然后将NSOperation对象添加到NSOperationQueue中 ...