关于IOS多线程
1个cpu执行的cpu命令列为一条无分叉路径,即为线程
如果这种无分叉路径有多条,这就是多线程
在学习操作系统的时候学过,cpu的抢占机制–时间片轮转
所以多线程程序的某个线程可以和其它多个线程反复进行上下文切换,这就看上去像1个cpu核能够并列执行多个线程一样
多线程的优点也是大于缺点的,在执行长时间的处理时仍能保证用户界面的响应性能
//不会阻塞主线程,异步的,可以先执行后面代码
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//会阻塞主线程等待block中执行完毕
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
});
//串行队列
dispatch_queue_t serial_queue = dispatch_queue_create("seral", DISPATCH_QUEUE_SERIAL);
//主队列 属于串行的队列 serial,由于主线程只有一个,所以它是串行的,可以思考一下
dispatch_queue_t main_queue = dispatch_get_main_queue();
//异步 全局(并行)
dispatch_async(global_queue_height, ^{
//并行任务
dispatch_async(main_queue, ^{
//主队列任务
});
});
//主线程同步会出现死锁,阻塞主线程,所以只能使用主线程异步
dispatch_async(main_queue, ^{
//主队列任务
});
并行队列
//全局队列 属于并行队列 concurrent
dispatch_queue_t global_queue_height = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
//并行队列
dispatch_queue_t concurrnt_queue = dispatch_queue_create("concurrent", DISPATCH_QUEUE_CONCURRENT);
```c
全局队列和主队列是IOS自定的两个队列
dispatch_queue_t global_queue_height = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0);
全局队列的优先级 DISPATCH_QUEUE_PRIORITY_HIGH,DISPATCH_QUEUE_PRIORITY_LOW,DISPATCH_QUEUE_PRIORITY_DEFAULT,DISPATCH_QUEUE_PRIORITY_BACKGROUND有四种种优先级
## 5.dispatch_set_target_queue 变更两个队列的优先级方法 ##
## 6.dispatch_after ##
并不是在指定时间后执行处理,而是到指定时间追加处理到队列(queue)中去执行
在下面例子中由于在主队列(main queue)的Runloop中执行。
第二个参数也可以指定具体某时某刻
```c
dispatch_time_t outtime = dispatch_time(DISPATCH_TIME_NOW, 15ull*NSEC_PER_SEC);
dispatch_after(outtime, dispatch_get_main_queue(), ^{
//15ull后添加到队列中
});
追加到队列中的任务如果想在结束之后进行处理,可以将其放入group中
dispatch_group_t group_t = dispatch_group_create();
dispatch_group_async(group_t, queue_global12, ^{
});
dispatch_group_async(group_t, queue_global12, ^{
});
//队列执行完成后调用
dispatch_group_notify(group_t, queue_global12, ^{
});
dispatch_group_wait(group_t, DISPATCH_TIME_FOREVER);
dispatch_time_t mytime = dispatch_time(DISPATCH_TIME_NOW, 1ull*NSEC_PER_SEC);
long result = dispatch_group_wait(group_t, mytime);
if (result==0) {
//group中的全部处理结束
}else{
//group的某一个处理还在进行中
}
dispatch_barrier_sync(global_queue1, ^{
//等到追加到concurrent dispatch queue 执行完毕之后才将制定的处理追加到指定的queue
});
dispatch_apply(10, global_queue1, ^(size_t index) {
NSLog(@"%zu",index);
});
//挂起
dispatch_suspend(global_queue1);
// 恢复
dispatch_resume(global_queue1);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 并行执行的线程1
});
dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{
// 并行执行的线程2
});
dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{
// 两个线程都执行完
});
最后关于一个缓存类,缓存有个特点数据频繁读取,写入次数有限,如果使用互斥步锁(NSLock,@synichronized,dispatch_semaphore_t等)资源消耗比较多,可以了解读写锁相关的内容
使用dispatch_queue_create创建的队列在并行多任务情况下,dispatch_barrier_async函数提交的任务会等它前面的任务执行完才开始,然后它后面的任务必须等它执行完毕才能开始.
@interface ICatche : NSObject
+(instancetype)sharedCatch;
- (id)cacheWithKey:(id)key;
-(void)setCatcheObject:(id)obj withKey:(id)key;
@end
#import "ICatche.h"
@interface ICatche()
{
dispatch_queue_t queue;
}
@property(nonatomic,strong)NSMutableDictionary *tempDic;
@end
@implementation ICatche
static ICatche *instance;
+(instancetype)sharedCatch{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc]init];
});
return instance;
}
+(instancetype)allocWithZone:(struct _NSZone *)zone{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [super allocWithZone:zone];
});
return instance;
}
-(instancetype)init{
if (self=[super init]) {
queue = dispatch_queue_create("Icatch_queue", DISPATCH_QUEUE_CONCURRENT);
self.tempDic = [NSMutableDictionary dictionaryWithCapacity:20];
}
return self;
}
//读取数据
- (id)cacheWithKey:(id)key{
__block id obj;
dispatch_sync(queue, ^{
obj = [self.tempDic objectForKey:key];
});
return obj;
}
//写入数据
-(void)setCatcheObject:(id)obj withKey:(id)key{
dispatch_barrier_async(queue, ^{
[self.tempDic setObject:obj forKey:key];
});
}
@end
大神唐巧文章传送门
[NSThread的用法](https://www.jianshu.com/p/686dbf4bbb52)