一个进程包括一个或者多个线程
进程:正在运行的程序
线程:最基本的执行单元
多个线程优点在于:提高执行效率,适当提高资源利用率
缺点:线程太多,占用CPU,内存
1. > pthread和GCD的区别
pthread和GCD基于C语言之上的,pthread生命周期要程序员管理 ,而后者不用
2. >NSThread和NSOperation的区别
NSThread和NSOperation基于OC语言之上的NSThread生命周期要程序员管理 ,而后者不用
在ios开发中.GCD和NSOperation会用的比较多
GCD:不用管周期 多核优化
NSOperation:基于GCD 实现了GCD不好实现的功能
下面简单说下用法:
生命周期:新建->可调度线程池—>运行—>阻塞—>就绪—>运行—>Death
NSThread:
1.对象方法
NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(task) object:nil];
//开启线程
[thread start];
//开启线程
[thread start];
2.类方法detach
[NSThread detachNewThreadSelector:@selector(task) toTarget:self withObject:nil];
3.隐式创建 : 用的比较多
[self performSelectorInBackground:@selector(task) withObject:nil];
阻塞 :
当多个异步任务在执行导入过程中
,
有一个特殊的任务要运行
,
当他运行的时候
,
其他任务不能运行
(
阻塞
),
运行完成之后居
,
其他的异步任务恢复并发执行
dispatch_barrier_async(concurrentQueue, ^{
[NSThread sleepForTimeInterval:1];
});