进程的基本概念
每个进程都是一个应用程序,都有独立的内存空间,一般来说一个应用程序存在一个进程,但也有多个进程的情况
同一个进程中的线程共享内存中内存和资源
多线程的基本概念
每个程序都有一个主线程,程序启动时创建(调用main来启动)
主线程的生命周期是和应用程序绑定的,程序退出(结束)时,主线程也停止了
多线程技术表示,一个应用程序有多个线程,使用多线程能提供CPU的使用率,防止主线程堵塞
任何有可能堵塞主线程的任务不要在主线程执行(访问网络)
线程相当于一个路径,多线程相当于一个多分枝路径。
对线程来说,主线程和子线程是并发的,对CPU来说是单个执行的(单核CPU)
即使子线程堵塞也不会影响主线程。
1.第一种开启新的线程调用 mutableThread
NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector(mutableThread) object:nil];
[t star];//需要手动开启
2.第二种开启新的线程调用 mutableThread
[NSThread datachNewThreadSelect:@select(mutableThread) toTarget:self withObject:nil];
3.第三种开启新的线程调用 mutableThread
[self performSelectorInBackground:@selector(mutableThread) withObject:nil];
4.第四种,block语法启动一个线程
NSOperationQueue *threadQueue = [[NSOperationQueue alloc] init];
[threadQueue addOperationWithBlock:^(void){
//要写的内容
}];
5.NSOperation开启一个线程
NSOperationQueue *threadQueue = [[NSOperationQueue alloc] init];
threadQueue.maxConucrrentOperationCount = 1;//设置线程的并发数,当然还可以设置优先级(op.queuePriority
= NSOperation QueuePriorityHigh)。
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector
(mutableThread) object:nil];
[threadQueue addOperation:op];//可以添加多个线程
//在主线程上调用reloadData 方法
[self performSelectorOnainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
//这个方法是从子线程中返回主线程
[self performSelectorOnMainThread:@selector(mainThread) withObject:nil waitUntilDone:YES];
-(void)mainThread{
.//这个是返回到主线程里面,然后编写代码
}
多线程的常用方法
NSThread的常用方法
//判断当前线程是否是多线程
+(BOOL)isMultiThreaded;
//获取当前线程对象
+(NSThread *)currentThread;
//是当前线程睡眠指定的时间,单位为秒
+(void)sleepForTimeInterval:(NSTimeInterval)ti;
//退出当前线程
+(void)exit;
//判断当前线程是否为主线程
+(BOOL)isMainThread
//启动该线程
-(void)start;
GCD(Grand Central Dispatch)
GCD是一个大的主题。它可以提高代码执行效率与多核的利用率。
//创建一个队列(线程池)
dispatch_queue_t queue = dispatch_queue_create("test",NULL);
//创建异步线程
dispatch_async(queue,^{
//多线程
//回到主线程执行
dispatch_sync(dispatch_get_main_queue(),^{
//主线程
}) ;
});
//创建同步线程
dispatch_sync(queue,^{})
在我们创建子线程的时候需要添加自动释放池管理内存
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release];
线程的生命周期纯在五个状态:新建、就绪、运行、阻塞、死亡
NSRunLoop可以保持一个线程一直为活动状态,不会马上销毁。
每个进程都是一个应用程序,都有独立的内存空间,一般来说一个应用程序存在一个进程,但也有多个进程的情况
同一个进程中的线程共享内存中内存和资源
多线程的基本概念
每个程序都有一个主线程,程序启动时创建(调用main来启动)
主线程的生命周期是和应用程序绑定的,程序退出(结束)时,主线程也停止了
多线程技术表示,一个应用程序有多个线程,使用多线程能提供CPU的使用率,防止主线程堵塞
任何有可能堵塞主线程的任务不要在主线程执行(访问网络)
线程相当于一个路径,多线程相当于一个多分枝路径。
对线程来说,主线程和子线程是并发的,对CPU来说是单个执行的(单核CPU)
即使子线程堵塞也不会影响主线程。
1.第一种开启新的线程调用 mutableThread
NSThread *t = [[NSThread alloc] initWithTarget:self selector:@selector(mutableThread) object:nil];
[t star];//需要手动开启
2.第二种开启新的线程调用 mutableThread
[NSThread datachNewThreadSelect:@select(mutableThread) toTarget:self withObject:nil];
3.第三种开启新的线程调用 mutableThread
[self performSelectorInBackground:@selector(mutableThread) withObject:nil];
4.第四种,block语法启动一个线程
NSOperationQueue *threadQueue = [[NSOperationQueue alloc] init];
[threadQueue addOperationWithBlock:^(void){
//要写的内容
}];
5.NSOperation开启一个线程
NSOperationQueue *threadQueue = [[NSOperationQueue alloc] init];
threadQueue.maxConucrrentOperationCount = 1;//设置线程的并发数,当然还可以设置优先级(op.queuePriority
= NSOperation QueuePriorityHigh)。
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector
(mutableThread) object:nil];
[threadQueue addOperation:op];//可以添加多个线程
//在主线程上调用reloadData 方法
[self performSelectorOnainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
//这个方法是从子线程中返回主线程
[self performSelectorOnMainThread:@selector(mainThread) withObject:nil waitUntilDone:YES];
-(void)mainThread{
.//这个是返回到主线程里面,然后编写代码
}
多线程的常用方法
NSThread的常用方法
//判断当前线程是否是多线程
+(BOOL)isMultiThreaded;
//获取当前线程对象
+(NSThread *)currentThread;
//是当前线程睡眠指定的时间,单位为秒
+(void)sleepForTimeInterval:(NSTimeInterval)ti;
//退出当前线程
+(void)exit;
//判断当前线程是否为主线程
+(BOOL)isMainThread
//启动该线程
-(void)start;
GCD(Grand Central Dispatch)
GCD是一个大的主题。它可以提高代码执行效率与多核的利用率。
//创建一个队列(线程池)
dispatch_queue_t queue = dispatch_queue_create("test",NULL);
//创建异步线程
dispatch_async(queue,^{
//多线程
//回到主线程执行
dispatch_sync(dispatch_get_main_queue(),^{
//主线程
}) ;
});
//创建同步线程
dispatch_sync(queue,^{})
在我们创建子线程的时候需要添加自动释放池管理内存
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[pool release];
线程的生命周期纯在五个状态:新建、就绪、运行、阻塞、死亡
NSRunLoop可以保持一个线程一直为活动状态,不会马上销毁。