iOS常用的多线程:GCD、NSOperation、NSThread
GCD
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"aaaa");
});// 全局队列
//串行队列
dispatch_queue_t serial_queue = dispatch_queue_create("lipo", DISPATCH_QUEUE_SERIAL);
//并发队列
dispatch_queue_t concurrent_queue = dispatch_queue_create("lipo", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(serial_queue, ^{
NSLog(@"currentThread_A:%@",[NSThread currentThread]);
});
dispatch_async(concurrent_queue, ^{
NSLog(@"currentThread_B :%@",[NSThread currentThread]);
});
//线程阻塞 concurrent_queue要一致才能起作用
dispatch_barrier_async(concurrent_queue, ^{
NSLog(@"currentThread_C:%@",[NSThread currentThread]);
});
dispatch_async(concurrent_queue, ^{
NSLog(@"currentThread_D:%@",[NSThread currentThread]);
});
//A、B、C三个线程,执行完AB后再执行C
dispatch_group_t grop = dispatch_group_create();
dispatch_group_async(grop, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"耗时操作1 %@",[NSThread currentThread]);
sleep(3);
});
dispatch_group_async(grop, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"耗时操作2 %@",[NSThread currentThread]);
sleep(3);
});
dispatch_group_notify(grop, dispatch_get_main_queue(), ^{
NSLog(@"操作3 %@",[NSThread currentThread]);
});
//快速迭代,类似for循环
dispatch_apply(5, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t index) {
NSLog(@"任务执行 快速迭代 %@",[NSThread currentThread]);
});
NSLog(@"任务执行结束");
//GCD的延时操作
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
NSLog(@"延时5秒后执行 %@",[NSThread currentThread]);
});
//只执行一次任务,如单例创建
static dispatch_once_t oneceToken;
dispatch_once(&oneceToken, ^{
NSLog(@"只执行一次");
});
信号量
@interface Dispach_semaphore()
{
dispatch_semaphore_t _semaphore;
dispatch_group_t _grope;
dispatch_queue_t _queue;
}
@end
@implementation Dispach_semaphore
- (instancetype)init
{
self = [super init];
if (self) {
_semaphore = dispatch_semaphore_create(2);
_grope = dispatch_group_create();
_queue = dispatch_queue_create("com.lipo.www",DISPATCH_QUEUE_CONCURRENT);
}
return self;
}
-(void)addTask{
dispatch_async(_queue, ^{
dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_semaphore_signal(_semaphore);
});
});
}
多度单写
@interface UserCenter()
{
// 定义一个并发队列
dispatch_queue_t concurrent_queue;
// 用户数据中心, 可能多个线程需要数据访问
NSMutableDictionary *userCenterDic;
}
@end
// 多读单写模型
@implementation UserCenter
- (id)init
{
self = [super init];
if (self) {
// 通过宏定义 DISPATCH_QUEUE_CONCURRENT 创建一个并发队列
concurrent_queue = dispatch_queue_create("read_write_queue", DISPATCH_QUEUE_CONCURRENT);
// 创建数据容器
userCenterDic = [NSMutableDictionary dictionary];
}
return self;
}
- (id)objectForKey:(NSString *)key
{
__block id obj;
// 同步读取指定数据
dispatch_sync(concurrent_queue, ^{
obj = [userCenterDic objectForKey:key];
});
return obj;
}
- (void)setObject:(id)obj forKey:(NSString *)key
{
// 异步栅栏调用设置数据
dispatch_barrier_async(concurrent_queue, ^{
[userCenterDic setObject:obj forKey:key];
});
}
NSOperation
NSOperation 需要和NSOperationQueue配合使用来实现多线程方案
特点:
1.添加任务依赖 :addDependency / removeDependency
2.任务执行状态控制 isReady isExecuting isFinished isCancelled
重写main方法底层控制任务状态
重写start方法需要自己控制任务状态
系统是怎样移除一个isFinish=yes的nsoperation的? -> KVO
KVO实现机制和原理?
3.最大并发量 setMaxConcurrentOperationCount 当为1的时候相当于串型
NSOperation添加操作依赖和监听
//创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
//封装操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1-----------%@",[NSThread currentThread]);
sleep(2);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"2-----------%@",[NSThread currentThread]);
sleep(2);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"3-----------%@",[NSThread currentThread]);
sleep(2);
}];
NSBlockOperation *op4 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"4-----------%@",[NSThread currentThread]);
sleep(2);
}];
op3.completionBlock = ^{//添加监听,操作执行完毕后 回调
NSLog(@"op3执行完毕。。");
};
//添加操作依赖 4->3->2->1 // 添加操作依赖能控制多任务并发的执行顺序,不能设置循环依赖,可以跨队列添加依赖
[op3 addDependency:op4];
[op2 addDependency:op3];
[op1 addDependency:op2];
//[op2 removeDependency:op3];//删除依赖
//添加到队列中
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue addOperation:op4];
NSThread
//创建和启动线程,一个NSThread对象就代表一条线程 。
NSThread *thread = [[NSThread alloc] initWithTarget:selfselector:@selector(run) object:nil];
[thread start];
//线程一启动,就会告诉 CPU 准备就绪,可以随时接受 CPU 调度!CPU 调度当前线程之后,就会在线程thread中执行self的run方法
//主线程相关用法
+ (NSThread *)mainThread; 获得主线程
- (BOOL)isMainThread; 是否为主线程
//获得当前线程
NSThread *current = [NSThread currentThread];
//线程的调度优先级
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
- (double)threadPriority;
- (BOOL)setThreadPriority:(double)p;
//调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高
//线程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
//其他创建线程方式,创建线程后自动启动线程
[NSThread detachNewThreadSelector:@selector(run) toTarget:selfwithObject:nil];
// 隐式创建并启动线程
[self performSelectorInBackground:@selector(run) withObject:nil];
//上述2种创建线程方式的优缺点
//优点:简单快捷
//缺点:无法对线程进行更详细的设置
//控制线程状态
//启动线程
- (void)start;
//进入就绪状态 ->运行状态。当线程任务执行完毕,自动进入死亡状态
//阻塞(暂停)线程
+ (void)sleepUntilDate:(NSDate *)date;
+ (void)sleepForTimeInterval:(NSTimeInterval)ti;
//进入阻塞状态
//强制停止线程
+ (void)exit;//进入死亡状态
//注意:一旦线程停止(死亡)了,就不能再次开启任务