那么今天我就来谈谈什么是多线程编程,一个应用程序我们称为一个进程,在程序运行的时候我们会给他分配内存空间,一个进程由许多线程组成,说白了线程就是用来跑应用程序的。
一、关于线程我们有几点需要注意:
1.一个进程里面至少有一个线程,这个线程称为主线程。
2.主线程负责执行程序中的所有代码,代码只能书怒执行,无法并发执行(同步)。
3.进程中有很多个线程,线程之间互补干涉,为了同一个功能有许多线程运行。
二、多线程:
1.首先什么时候用多线程?
大量计算、数据读取(本地数据库查询所有数据)、网络请求(同步)
2.在IOS中由用户开辟的,相对于主线程来讲,称为子线程。线程与线程之间称为并发。
三、IOS中多线程实现的几种类型:
NSThread
首先创建一个多线程
NSThread的两种创建方法
1.(手动运行方式)
NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(方法名)object:nil];
start];
2. (自动运行方式)
[selfperformSelectorInBackground:@selector(方法名)withObject:self];
二、NSObject
后台执行某个方法
[selfperformSelectorInBackground:@selector(jisuan)withObject:self];
三、NSOperation和NSOperationQueue
NSOperation类,因为他是抽象的,不能直接使用,通常与NSOperationQueue结合使用。所以我们要自定义一个类继承于NSOperation,在方法中调用
*diy = [[DIYOperationalloc]init];
DIYOperation *second = [[DIYOperationalloc]init];
NSOperationQueue *queue = [[NSOperationQueuealloc]init];
[queue setMaxConcurrentOperationCount:1];
// 最大并行数为1的时候,线程与线程之间是同步的
addOperation:diy];
addOperation:second];
release];
release];
release];
// NSBlockOperation是NSOperation的子类
// block里面的内容就是多线程所要执行的代码
NSBlockOperation *oopr = [NSBlockOperationblockOperationWithBlock:^{
@autoreleasepool {
for (int i =0; i <100; i ++) {
NSLog(@"%d", i);
}
}
}];
NSOperationQueue
addOperation:oopr];
release];
NSOperation的子类
//封装了执行操作的target和要执行的action
NSInvocationOperation *nsin = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(createImage:)object:nil];
addOperation:nsin];
release];
注意:在多线程方法中应添加自动释放池,我们手动添加的子线程,需要我们手动添加自动释放池。
四、GCD
// 创建一个同步的线程队列
// 在GCD中没有自动释放池
- (void)createSerialGCD
{
// 第一步:创建一个同步线程队列
dispatch_queue_t queue =dispatch_queue_create("first",DISPATCH_QUEUE_SERIAL);
// 第二步:异步执行同步线程队列(与主线程异步)
dispatch_async(queue, ^{
// 多线程的代码
// 下载一张图片,并显示到界面上,(下载用同步下载)
//http://h.hiphotos.baidu.com/zhidao/pic/item/f3d3572c11dfa9ec5d1e33ee60d0f703918fc113.jpg
NSString *path =@"http://h.hiphotos.baidu.com/zhidao/pic/item/f3d3572c11dfa9ec5d1e33ee60d0f703918fc113.jpg";
NSURL *url = [NSURLURLWithString:path];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
setHTTPMethod:@"GET"];
NSURLResponse *response =nil;
NSError *error =nil;
NSData *data = [NSURLConnectionsendSynchronousRequest:request returningResponse:&response error:&error];
UIImage *image = [UIImageimageWithData:data];
// 显示到界面上,所有跟UI有关的内容全部都要在主线程上运行
// 返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 在主线程运行
UIImageView *imaView = [[UIImageViewalloc]initWithImage:image];
self.viewaddSubview:imaView];
release];
});
});
}
- (void)createConcurrentGCD
{
// 可以并发的执行的执行多个任务,但是要遵守FIFO(FirstInFirstOut)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
NSURL *url = [NSURLURLWithString:@"http://h.hiphotos.baidu.com/zhidao/pic/item/f3d3572c11dfa9ec5d1e33ee60d0f703918fc113.jpg"];
// 同步下载
NSData *data = [NSDatadataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imaView = [[UIImageViewalloc]initWithImage:[UIImageimageWithData:data]];
self.viewaddSubview:imaView];
release];
});
});
}