- 一丶多线程概述
一个应用程序就是一个进程; (包含多个子线程) 一个进程包括多个线程 ;
线程与线程之间可以共享进程的内存区域
iOS中关于UI的添加必须在主线程中操作 .
- 二丶NSThread
轻量级多线程,两种创建方法 实例化需要手动开启子线程 类方法初始化的时候已经开启;
注意: 在多线程方法中需要添加到自动释放池 2,在应用程序打开的时候,系统会自动为主线程创建一个自动释放池,而我们手动创建子线程,
//创建子线程-(void)createThread
{
NSLog(@"..........");
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(btnAction) object:nil];
[thread start];
[thread release];
}
[NSThread detachNewThreadSelector:@selector(btnAction) toTarget:self withObject:nil];
- 三丶NSOperation(基类 )
只能继承,用它的子类
//创建nsoperation线程
-(void)createOperation
{
NSInvocationOperation *invacation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnAction) object:nil];
[invacation start];
[invacation release];
// [invacation start];
// [invacation release];
NSInvocationOperation *invacation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnAction) object:nil];
NSInvocationOperation *invacation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnAction) object:nil];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:invacation];
[queue addOperation:invacation1];
[queue addOperation:invacation2];
[queue setMaxConcurrentOperationCount:3]; //最大并发数
[invacation release];
[invacation1 release];
[invacation2 release];
[queue release];
//后台执行某个方法 //系统自动给创建一个子线程,执行任务完成后自动回收
//GCD 返回主线程
-(void)backMainThread:(id)sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__block int sum = 0;
for (int i = 0; i < 10000; i++) {
sum += i;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"sum ======%d",sum);
});
});
//顺序执行
dispatch_queue_t serialQueue = dispatch_get_main_queue();
dispatch_async(serialQueue, ^{
NSLog(@"第1个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(serialQueue, ^{
NSLog(@"第2个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(serialQueue, ^{
NSLog(@"第3个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
//创建并行线程队列
-(void)concurrentButton:(id)sender
{
//1,获取系统的dispatch_global_queue
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //(线程优先级.共4种), (要预留参数)
-(void)createOperation
{
NSInvocationOperation *invacation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnAction) object:nil];
[invacation start];
[invacation release];
}
NSOperationQueue 线程并发
NSInvocationOperation *invacation
= [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnAction) object:nil];// [invacation start];
// [invacation release];
NSInvocationOperation *invacation1 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnAction) object:nil];
NSInvocationOperation *invacation2 = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(btnAction) object:nil];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[queue addOperation:invacation];
[queue addOperation:invacation1];
[queue addOperation:invacation2];
[queue setMaxConcurrentOperationCount:3]; //最大并发数
[invacation release];
[invacation1 release];
[invacation2 release];
[queue release];
} 先放进去,先执行 并发的时候,现执行前面的
NSObject 中存了一个最简单的后台执行方法
与NSThread 类创建 区别 手动创建,指定方法
-(void)createObject
{
//系统爱怎么做就怎么做
[self performSelectorInBackground:@selector(btnAction) withObject:nil];
-(void)createObject
{
//系统爱怎么做就怎么做
[self performSelectorInBackground:@selector(btnAction) withObject:nil];
}
返回主线程方法
GCD: dispatch_get_main_queue()
- NSObject: [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
下载完成后用performSelectorOnMainThread执行主线程updateUI方法。
两个线程之间进行通信:
[self performSelector:@selector(mainAction:) onThread:[NSThread mainThread] withObject:[NSNumber numberWithInt:sum] waitUntilDone:YES];
-(void)backMainThread:(id)sender
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__block int sum = 0;
for (int i = 0; i < 10000; i++) {
sum += i;
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"sum ======%d",sum);
});
});
}
- 四丶GCD
先进先执行,后进后执行.
GCD中三种队列:
dispatch queue分为下面三种:
1.Serial:只执行一个任务,Serial queue通常用于同步访问特定的资源或者数据,当你创建多个时,各自同步执行,但Serial queue之间是并发执行的
//1 获取系统的串行队列,就是主线程的//顺序执行
dispatch_queue_t serialQueue = dispatch_get_main_queue();
dispatch_async(serialQueue, ^{
NSLog(@"第1个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(serialQueue, ^{
NSLog(@"第2个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(serialQueue, ^{
NSLog(@"第3个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
}
2.Concurrent: 可以并发执行多个任务,但是执行完的顺序是随机的
-(void)concurrentButton:(id)sender
{
//1,获取系统的dispatch_global_queue
dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //(线程优先级.共4种), (要预留参数)
//先执行Block块里的功能
dispatch_async(globalQueue, ^{
NSLog(@"第一个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(globalQueue, ^{
NSLog(@"第二个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(globalQueue, ^{
NSLog(@"第三个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(globalQueue, ^{
NSLog(@"第一个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(globalQueue, ^{
NSLog(@"第二个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
dispatch_async(globalQueue, ^{
NSLog(@"第三个任务,所在线程是%@,是否是主线程%d",[NSThread currentThread],[[NSThread currentThread]isMainThread]);// (线程,是否主线程)
});
//2,自己创建并行队列
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("lanou", DISPATCH_QUEUE_CONCURRENT);//(C#不可变字符串线程队列名字,串行或者并行)
dispatch_async(myConcurrentQueue, ^{
NSLog(@"计算结果:");
[self action1];
dispatch_queue_t myConcurrentQueue = dispatch_queue_create("lanou", DISPATCH_QUEUE_CONCURRENT);//(C#不可变字符串线程队列名字,串行或者并行)
dispatch_async(myConcurrentQueue, ^{
NSLog(@"计算结果:");
[self action1];
任务
});
});
}
3.Main dispatch queue:全局可用,它使应用程序主线上执行任务的
- 五丶线程同步 (串行, 一个执行完,在执行另外一个)
同步就是协同,按预定的先后顺序执行. 线程同步,多个线程,一个程序运行结束后,下一个线程开始运行.
1,Serial queue 2, [queue setMaxConcurrentOperationCount:1];
总结: 多线程使用场景: 同步网络连接 (下载图片)丶 数据库读取数据丶大量循环等等 比较繁琐的事
注意情况 : UI界面相关内容必须返回主线程 | 在线程中添加自动释放池(处理对象)
版权声明:本文为博主原创文章,未经博主允许不得转载。