浅谈iOS多线程编程
来源:互联网 发布:安卓系统还原软件 编辑:程序博客网 时间:2024/05/01 17:33
那么今天我就来谈谈什么是多线程编程,一个应用程序我们称为一个进程,在程序运行的时候我们会给他分配内存空间,一个进程由许多线程组成,说白了线程就是用来跑应用程序的。
一、关于线程我们有几点需要注意:
1.一个进程里面至少有一个线程,这个线程称为主线程。
2.主线程负责执行程序中的所有代码,代码只能书怒执行,无法并发执行(同步)。
3.进程中有很多个线程,线程之间互补干涉,为了同一个功能有许多线程运行。
二、多线程:
1.首先什么时候用多线程?
大量计算、数据读取(本地数据库查询所有数据)、网络请求(同步)
2.在IOS中由用户开辟的,相对于主线程来讲,称为子线程。线程与线程之间称为并发。
三、IOS中多线程实现的几种类型:
一 、NSThread
首先创建一个多线程
NSThread的两种创建方法
1.(手动运行方式)
NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(方法名)object:nil];
[thread start];
2. (自动运行方式)
[selfperformSelectorInBackground:@selector(方法名)withObject:self];
二、NSObject
后台执行某个方法
[selfperformSelectorInBackground:@selector(jisuan)withObject:self];
DIYOperation *diy = [[DIYOperationalloc]init];
DIYOperation *second = [[DIYOperationalloc]init];
NSOperationQueue *queue = [[NSOperationQueuealloc]init];
[queue setMaxConcurrentOperationCount:1];
//最大并行数为1的时候,线程与线程之间是同步的
[queue addOperation:diy];
[queue addOperation:second];
[queue release];
[diy release];
[second release];
// NSBlockOperation是NSOperation的子类
// block里面的内容就是多线程所要执行的代码
NSBlockOperation *oopr = [NSBlockOperationblockOperationWithBlock:^{
@autoreleasepool {
for (int i =0; i <100; i ++) {
NSLog(@"%d", i);
}
}
}];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:oopr];
[queue release];
//NSInvocationOperation是NSOperation的子类
//封装了执行操作的target和要执行的action
NSInvocationOperation *nsin = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(createImage:)object:nil];
[queue addOperation:nsin];
[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];
[request setHTTPMethod:@"GET"];
NSURLResponse *response =nil;
NSError *error =nil;
NSData *data = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:&response error:&error];
UIImage *image = [UIImageimageWithData:data];
//显示到界面上,所有跟UI有关的内容全部都要在主线程上运行
// 返回主线程
dispatch_async(dispatch_get_main_queue(), ^{
// 在主线程运行
UIImageView *imaView = [[UIImageViewalloc]initWithImage:image];
[self.viewaddSubview:imaView];
[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];
[imaView release];
});
});
}
- 浅谈iOS多线程编程
- iOS多线程编程——浅谈GCD
- iOS多线程编程——浅谈NSOperation
- iOS开发探索-多线程(NSThread、NSOperation、GCD)编程浅谈
- 浅谈IOS多线程
- 浅谈IOS多线程问题
- 浅谈iOS中的多线程
- 浅谈多线程编程
- 浅谈多线程编程
- 浅谈Android多线程编程
- ios多线程浅谈和使用
- 浅谈Windows中的多线程编程
- 浅谈Qt中多线程编程
- 浅谈多线程、许可区编程
- iOS多线程编程指南
- IOS多线程编程指南
- [ios]多线程编程指南
- ios多线程编程
- 橙子引擎携多款精品TV游戏诚意呈献 2014智能电视游戏品牌峰会
- Android开发指南——Data Storage
- Microsoft .NET Micro Framework 4.0语法
- proj4的windows下编译
- vi显示行号 & vi查找命令
- 浅谈iOS多线程编程
- setjmp()与longjmp()
- Linux下查看文件大小
- Combination Sum II
- iOS开发内存警告Memory Warning和ViewController的生命周期的问题
- 以太网交换机中FID的概念和作用
- BZOJ系列1257《[CQOI2007]余数之和sum》题解
- 牛顿勘根法
- Linux工作队列实现机制