IOS线程数据篇2之多线程:NSThread NSOperation GCD(一)
来源:互联网 发布:relieff算法原理 编辑:程序博客网 时间:2024/05/16 19:39
栈空间是非常宝贵的,主线程只能开辟1M。子线程只有512K。android的栈空间好像只有2M。android的堆空间有16M、32M、64M的。
线程运行完之后,ARC下内存自动回收。线程不能被杀死,只能暂停。
优点:将耗时的任务分配到其他线程执行,由主线程负责统一更新界面会使应用程序更加流畅,用户体验更好
缺点:新建线程会消耗内存空间和CPU时间,线程太多会降低系统的运行性能。
多线程技术是为了并发执行多项任务,不会提高单个算法本身的执行效率。
IOS下线程使用的三种方式:
NSThread:简单使用比较方便,但线程管理比较复杂,一般不用。
NSOperation/NSOperationQueue :是使用GCD实现的一套Objective-C的API,提供了一些在GCD中不容易实现的特性,如:限制最大并发数量、操作之间的依赖关系
GCD —— Grand Central Dispatch 大中央 调度分发。是基于C语言的底层API,用Block定义任务。
GCD的基本思想是就将操作s放在队列s中去执行,队列就是负责调度的!谁空闲,就把任务给谁!
1 先看这个简单的NSThread:
虽然这个比较简单,在线程之间管理不方便,但同时这个也提供了一些简单易用的方法:
在后台运行
-(void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
在主线程中运行
– performSelectorOnMainThread:withObject:waitUntilDone:
在某一个线程中执行
– performSelector:onThread:withObject:waitUntilDone:modes:
在当前的线程延迟执行
performSelector:withObject:afterDelay获取当前线程信息
[NSThread currentThread]
线程休眠一段时间[NSThread sleepForTimeInterval:2.0f];
performSelector:withObject:afterDelayNSThread线程的使用
NSThread*thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(mutableThread:)object:@"test"];
[thread start];
[NSThreaddetachNewThreadSelector:@selector(mutableThread:)toTarget:selfwithObject:nil];
2 队列的使用
队列常用在以下几个方面
// 利⽤用线程队列添加多个线程并指定线程的优先级
NSOperationQueue*queue = [[NSOperationQueue alloc]init];
NSInvocationOperation*thread01 = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(mutableThread01:)object:nil];
NSInvocationOperation*thread02 = [[NSInvocationOperationalloc]initWithTarget:selfselector:@selector(mutableThread02:)object:nil];
// 设置最⼤大并发数,以及线程的优先级别。
queue.maxConcurrentOperationCount=1;
thread02.queuePriority=NSOperationQueuePriorityHigh;
[queue addOperation:thread01];
[queue addOperation:thread02];
NSBlockOperation *threadop1 = [NSBlockOperationblockOperationWithBlock:^{
}];
NSBlockOperation *threadop2 = [NSBlockOperationblockOperationWithBlock:^{
}];
[threadop2addDependency:threadop1];
[queueaddOperation:op1];
[[NSOperationQueuemainQueue] addOperation:op4];
UI更新只能在主线程中使用。利用队列可以设置并发个数,可以设置优先级,可以设置依赖关系(可以跨队列)。
- IOS线程数据篇2之多线程:NSThread NSOperation GCD(一)
- IOS线程数据篇3之多线程:NSThread NSOperation GCD(二)
- iOS之多线程开发(NSThread,NSOperation,GCD)
- iOS开发之多线程NSThread,NSOperation,GCD
- iOS开发之多线程——NSThread、NSOperation、GCD
- iOS - 线程(NSThread / NSOperation / GCD)
- iOS线程-NSOperation,NSThread以及GCD
- IOS-线程操作之NSThread/NSOperation/GCD
- [绍棠] iOS线程介绍 NSThread, NSOperation, GCD
- iOS开发之多线程编程技术NSThread、NSOperation、GCD三者使用详解
- iOS开发之多线程技术(NSThread、OperationQueue、GCD)
- iOS开发之多线程技术(NSThread、OperationQueue、GCD)
- iOS 之多线程 NSThread
- iOS -- 线程 还有待补全--(关于线程死锁,解决办法等。。)(NSThread、NSOperation、GCD) -- ASI/AFNetWork
- ios多线程之NSThread,GCD,NSOperation以及线程同步
- ios多线程之NSThread,GCD,NSOperation以及线程同步
- ios多线程之NSThread,GCD,NSOperation以及线程同步
- ios多线程之NSThread,GCD,NSOperation以及线程同步
- 【下资源】2014传智播客王保明Linux培训泄露免币福利
- 一个大的含有50M个URL的记录,一个小的含有500个URL的记录,找出两个记录里相同的URL。
- scala_mark_001
- JavaEE技术一些简单介绍
- gdb常用命令
- IOS线程数据篇2之多线程:NSThread NSOperation GCD(一)
- 通过输入框中字符串的像素长度限制输入文字长度
- SpringMVC项目中web.xml中的节点加载顺序问题
- 机器学习之开源库大总结
- android线控广播/长按功能开发总结
- SAE
- 数据结构和算法系列17 图
- Android之system_server与zygote之作用
- 【畅言】从“码农”说起